持续警报

时间:2017-01-14 04:17:40

标签: android android-alarms

我正在尝试设置一个警报,这个闹钟会一直播放,因为我想让它在3天之后关闭,我知道Android会在此之前杀死我的应用。

在我的活动中

private void setAlarm() {
    //ContactAlarmReceiver contactAlarmReceiver = new ContactAlarmReceiver();
    //contactAlarmReceiver.setAlarm(getApplicationContext(), dateInMillis, phonenumberET.getText().toString(), firstnameET.getText().toString());
    AlarmManager am = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
    Intent alarmIntent = new Intent(MyConstants.BROADCAST_ACTION_CONTACT_ALARM);
    alarmIntent.putExtra("phone", phonenumberET.getText().toString());
    alarmIntent.putExtra("name", firstnameET.getText().toString());

    PendingIntent pi = PendingIntent.getBroadcast(this, 0, alarmIntent, 0);
    //am.setAndAllowWhileIdle(AlarmManager.RTC_WAKEUP, timeInMills, pi);
    if (Build.VERSION.SDK_INT >= 23) {
        am.setExactAndAllowWhileIdle(AlarmManager.RTC_WAKEUP, dateInMillis, pi);
    } else if (Build.VERSION.SDK_INT >= 19) {
        am.setExact(AlarmManager.RTC_WAKEUP, dateInMillis, pi);
    } else {
        am.set(AlarmManager.RTC_WAKEUP, dateInMillis, pi);
    }
    ComponentName component = new ComponentName(this, ContactAlarmReceiver.class);
    getPackageManager()
        .setComponentEnabledSetting(component,
            PackageManager.COMPONENT_ENABLED_STATE_ENABLED,
            PackageManager.DONT_KILL_APP);
}

清单:

<receiver
    android:name=".ContactAlarmReceiver"
    android:enabled="false">
    <intent-filter>
        <action android:name="com.example.johnbravado.CONTACTALARM" />
        <action android:name="com.example.johnbravado.NOTIFLTBTN" />
        <action android:name="com.example.johnbravado.NOTIFRTBTN" />
    </intent-filter>
</receiver>

ContactAlarmReceiver:

@Override
@Override
public void onReceive(Context context, Intent intent) {
    PowerManager powerManager = (PowerManager) context.getSystemService(POWER_SERVICE);
    PowerManager.WakeLock wakeLock = powerManager.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK,
            "com.example.johnbravado.zionwork");
    wakeLock.acquire();

    final NotificationManager mNotificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
    Bundle extras = intent.getExtras();
    ComponentName receiver = new ComponentName(context, ContactAlarmReceiver.class);
    PackageManager pm = context.getPackageManager();

    if (intent.getAction().equals(MyConstants.BROADCAST_ACTION_CONTACT_ALARM)) {
        int count = extras.size();
        String num = extras.getString("phone");
        String name = extras.getString("name");
        sendContactNotification(context, MyConstants.BIG_NOTIFICATION_ID, num, name);
    }

    if (intent.getAction().equals(MyConstants.BROADCAST_ACTION_NOTIF_LTBTN)) {
        sendSMS(context, extras.getString("phone"), extras.getString("msg"));
        mNotificationManager.cancel(MyConstants.BIG_NOTIFICATION_ID);
        pm.setComponentEnabledSetting(receiver,
                PackageManager.COMPONENT_ENABLED_STATE_DISABLED,
                PackageManager.DONT_KILL_APP);
    }

    if (intent.getAction().equals(MyConstants.BROADCAST_ACTION_NOTIF_RTBTN)) {
        initiatePhone(context, intent.getExtras().getString("phone"));
        mNotificationManager.cancel(MyConstants.BIG_NOTIFICATION_ID);
        pm.setComponentEnabledSetting(receiver,
                PackageManager.COMPONENT_ENABLED_STATE_DISABLED,
                PackageManager.DONT_KILL_APP);
    }
    wakeLock.release();
}

我从未在很长一段时间后收到通知。例如,我在晚上11点将警报设置为凌晨3点,所以当我醒来时,我应该看到通知。但没有显示任何通知。

1 个答案:

答案 0 :(得分:1)

我想建议你,首先要创建一个类:

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.widget.Toast;

public class AlarmReceiver extends BroadcastReceiver {

    @Override
    public void onReceive(Context context, Intent intent) {

        if(intent.getAction().equals("startalarm")){
        intent = new Intent(context, Your_service.class);
        context.startService(intent);

}

    }
}

注意:如果您的闹钟必须执行网络任务,请在警报广播的onRecieve()方法内启动下载服务。 然后,在Intent中设置AlarmReceiver类。

Intent alarmIntent = new Intent(MyActivity.this, AlarmReceiver.class);

也许你会从你的问题中解脱出来。