简体   繁体   中英

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?

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM