Android: Get all PendingIntents set with AlarmManager
You need to create your pending intent and then cancel it
AlarmManager alarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
Intent updateServiceIntent = new Intent(context, MyPendingIntentService.class);
PendingIntent pendingUpdateIntent = PendingIntent.getService(context, 0, updateServiceIntent, 0);
// Cancel alarms
try {
alarmManager.cancel(pendingUpdateIntent);
} catch (Exception e) {
Log.e(TAG, "AlarmManager update was not canceled. " + e.toString());
}
Listing all PendingIntents from AlarmManager
Step #1: Record all PendingIntents
that you register with AlarmManager
.
Step #2: Use the results from Step #1 for your cancel buttons.
There is no way to query AlarmManager
for scheduled alarms -- you have to track those yourself.
Get list of active PendingIntents in AlarmManager
adb shell dumpsys alarm > dump.txt
dump.txt:
Current Alarm Manager state:
Realtime wakeup (now=1309361618777):
RTC_WAKEUP #5: Alarm{4822f618 type 0 com.google.android.gsf}
type=0 when=1309882326582 repeatInterval=522747000 count=0
operation=PendingIntent{47dd3740: PendingIntentRecord{4822aeb8 com.google.android.gsf broadcastIntent}}
...
RTC #5: Alarm{4810f9d8 type 1 com.tmobile.selfhelp}
type=1 when=1309445979715 repeatInterval=86400000 count=1
operation=PendingIntent{4815a5c8: PendingIntentRecord{4810f960 com.tmobile.selfhelp startService}}
RTC #4: Alarm{4810f668 type 1 com.tmobile.selfhelp}
type=1 when=1309445959620 repeatInterval=86400000 count=1
operation=PendingIntent{480996e8: PendingIntentRecord{480214a0 com.tmobile.selfhelp broadcastIntent}}
...
Elapsed realtime wakeup (now=2110632):
ELAPSED_WAKEUP #5: Alarm{481c24e0 type 2 com.google.android.apps.maps}
type=2 when=2147485512925 repeatInterval=0 count=0
operation=PendingIntent{47d1d3a8: PendingIntentRecord{481a2600 com.google.android.apps.maps broadcastIntent}}
...
ELAPSED #1: Alarm{4829ce98 type 3 android}
type=3 when=2512653 repeatInterval=0 count=0
operation=PendingIntent{47eabda8: PendingIntentRecord{47f20250 android broadcastIntent}}
ELAPSED #0: Alarm{480f0198 type 3 com.mixzing.basic}
type=3 when=2439998 repeatInterval=0 count=0
operation=PendingIntent{48100dd8: PendingIntentRecord{480ff5a0 com.mixzing.basic broadcastIntent}}
Broadcast ref count: 0
Alarm Stats:
com.google.android.location
3ms running, 1 wakeups
1 alarms: act=com.google.android.location.ALARM_WAKEUP flg=0x4
com.google.android.gsf
274ms running, 4 wakeups
1 alarms: flg=0x4
1 alarms: act=com.google.android.intent.action.GTALK_RECONNECT flg=0x4
2 alarms: act=com.google.android.intent.action.GTALK_HEARTBEAT flg=0x4
...
-------------------------------------------------------------------------------
Android AlarmManager PendingIntent is launched before call to alarm.set()
You are using an alarm type of AlarmManager.RTC_WAKEUP
, which will set the alarm to start at a specified time, in your case millis
.
To set the alarm to start at a certain amount of time in the future, you need to either use one of these types of elapsed-time alarms:
- ELAPSED_REALTIME
Alarm time in SystemClock.elapsedRealtime() (time since boot,
including sleep). This alarm does not wake the device up; if it goes
off while the device is asleep, it will not be delivered until the
next time the device wakes up.
- ELAPSED_REALTIME_WAKEUP
Alarm time in SystemClock.elapsedRealtime() (time since boot,
including sleep), which will wake up the device when it goes off.
Or use an RTC
or RTC_WAKEUP
with the current time plus the time to wait, like:
alarm.set(AlarmManager.RTC_WAKEUP, Calendar.getInstance().getTimeInMillis() + millis, pi);
Android - What is the correct way of keeping track of multiple PendingIntents?
It is not possible to query the AlarmManager
for the PendingIntent
s. You must store the resultCode
and other information to recreate the PendingIntent
in order to delete it.
For example in SharedPreferences
or in a database like SqliteDB or Room. Then you can recreate the PendingIntent to cancel it.
See how to cancel PendingIntent.
See How does android compare pending intents for details on what fields are important when comparing the intents for deletion.
Related Topics
Android, Make an Image at a Url Equal to Imageview's Image
Custom Cut/Copy Action Bar for Edittext That Shows Text Selection Handles
Onsharedpreferencechanged Not Fired If Change Occurs in Separate Activity
Cardview Layout_Width="Match_Parent" Does Not Match Parent Recyclerview Width
Preferenceactivity Android 4.0 and Earlier
Navigationview and Custom Layout
How to Filter Android Logcat by Application
Set Span for Items in Gridlayoutmanager Using Spansizelookup
How to Add APKs in an Aosp Build
How to Get Name of Wifi-Network Out of Android Using Android API
Cancel Notification on Remove Application from Multitask Panel
How to Create Xxhdpi, Xhdpi, Hdpi, Mdpi and Ldpi Drawables from a Large Scale Image
Java.Lang.Noclassdeffounderror:Failed Resolution of :Lorg/Apache/Http/Protocolversion