Android:无法使用AlarmManager取消闹钟

时间:2018-05-14 05:42:58

标签: android alarmmanager

我正在使用它来设置闹钟,当我获取设备中设置的所有闹钟时,它就会显示。

    /**
     * Set alarm
     **/


    Intent alarmIntent = new Intent(context, LocalReminderReceiver.class);
    AlarmManager manager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
    long time = Long.valueOf("1526112720000");
    PendingIntent pendingIntent = PendingIntent.getBroadcast(context, 1, alarmIntent, FLAG_UPDATE_CURRENT);
    manager.set(AlarmManager.RTC_WAKEUP, time, pendingIntent);

    /**
     * Cancel alarm
     */
    PendingIntent sender = PendingIntent.getBroadcast(context, 1, alarmIntent, PendingIntent.FLAG_CANCEL_CURRENT);
    AlarmManager alarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
    alarmManager.cancel(sender);
    sender.cancel();

我错过了什么吗?

1 个答案:

答案 0 :(得分:2)

在线下方设置闹钟使用

 Intent intent = new Intent(context, AlarmReceiverActivity.class);

 PendingIntent pendingIntent = PendingIntent.getActivity(context, requestID, intent, 0);

 AlarmManager am = (AlarmManager)context.getSystemService(Activity.ALARM_SERVICE);
 am.set(AlarmManager.RTC_WAKEUP, timeInMillis, pendingIntent);

取消线下方使用警报

 AlarmManager am = (AlarmManager)context.getSystemService(Context.ALARM_SERVICE);
 Intent intent = new Intent(context, AlarmReceiverActivity.class);
 PendingIntent pendingIntent = PendingIntent.getActivity(context,requestID, intent, 0);
 am.cancel(pendingIntent);

以上代码的set和取消alram对我有用。 您必须通过相同的请求ID 来设置和取消特定警报