Friday, April 25, 2014

Testing that Android AlarmManager has an alarm set.

Just a small post from my recent experience - how to test that AlarmManager has an alarm set.

The first approach is to do it programmatically - let's assume we registered our alarm as below:
Intent intent = new Intent("");
PendingIntent pendingIntent = PendingIntent.getBroadcast(context, 0, 
                                      intent, PendingIntent.FLAG_UPDATE_CURRENT);
Calendar calendar = Calendar.getInstance();
calendar.add(Calendar.MINUTE, 1);

AlarmManager alarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), 1000 * 60, pendingIntent); 
And now to check that registered above alarm is active we have to do the following:
boolean alarmUp = (PendingIntent.getBroadcast(context, 0, 
        new Intent(""), 
        PendingIntent.FLAG_NO_CREATE) != null);

if (alarmUp) {
    Log.d("myTag", "Alarm is already active");
The idea is to use PendingIntent.FLAG_NO_CREATE which according to android documentation is a flag indicating that if the described PendingIntent does not already exist, then simply return null instead of creating it (

The second solution is to use adb shell command:

After you run this command from the command line:
adb shell dumpsys alarm | grep
you'll receive the list of active alarms for provided package name: +93ms running, 1 wakeups:
    +88ms 1 wakes 1 alarms:
More about dupmsys here -

Consider testing the alarm after:
  1. device restarted
  2. application was killed by the system or force stopped manually
  3. application upgrade

Happy testing!

Examples above were taken from this page -