Oderik
Oderik

Reputation: 2260

Alarm fires on "sleeping" Android device

I am writing an Android AppWidget that needs to be updated frequently (yes, I know, shouldn't do that, whatever...). I would like it to be updated only if it is visible, but annoyingly there seems to be nothing similar to WallpaperService.Engine.onVisibilityChanged(boolean visible).

The suggested approach here is to set up an alarm with AlarmManager.setRepeating(AlarmManager.RTC, firstShot, interval, pendingIntent) that triggers the update every interval milliseconds. This would at least not wake the device when it is asleep.

That's just what I did. My PendingIntent looks like this:

final Intent intent = new Intent(context, CountdownWidgetService.class);
intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_IDS, appWidgetIds);

final PendingIntent pendingIntent = PendingIntent.getService(context, 0, intent, PendingIntent.FLAG_CANCEL_CURRENT);

The CountdownWidgetService logs a message every time it is triggered by the alarm. The log message shows up even when the device should be asleep - but that's just what I thought was not supposed to happen. Where is my mistake?

Upvotes: 1

Views: 418

Answers (1)

Nikolay Elenkov
Nikolay Elenkov

Reputation: 52956

Maybe some other app you have is waking up the device, and your alarm gets executed with it. If you use AlarmManager.RTC that means: 'execute if the device is awake or wait until it is awake and execute then'. Check logcat to see what is happening and use this command to see what other alarms are registered:

adb shell dumpsys alarm

Upvotes: 1

Related Questions