在我的应用程序中,我在AlarmManager中设置提醒,据我所知,为了取消警报,我需要记住我为创建警报的待定意图提供的唯一ID。
有没有办法一般取消我的应用程序的所有闹钟,而不必记住该ID?
答案 0 :(得分:1)
来自doc AlarmManager.cancel()
删除具有匹配Intent的所有警报。任何类型的警报,其意图与此意图匹配(由filterEquals(Intent)定义)将被取消。
因此在创建Intent时使用Class name并将其设置为挂起的intent。它不需要使用id,它将匹配意图并取消所有警报。如下面的代码
AlarmManager alarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
// Create intent with you service class, which will be matched by alarm manager
Intent intent = new Intent(context, YourPendingIntentServiceClass.class);
// Set to pending intent
PendingIntent pendingIntent = PendingIntent.getService(context, 0, intent, 0);
// And cancel alarms
try {
// Cancel all alarm created with YourPendingIntentServiceClass.
alarmManager.cancel(pendingIntent);
} catch (Exception e) {
Log.e(TAG, "Something went wrong. Alarms did not cancel. " + e.toString());
}