Pre API 19, the go-to method for updating a Widget faster than the updatePeriodMillis
minimum time of 30 minutes was to use an AlarmManager
and a BroadcastReceiver
to receive the Intent after the specified interval used when setting up the AlarmManager.
Currently, using the below code, the Widget is updated, but as of Android 5.1, using .setRepeating() with a repeat interval of less than 60000ms will automatically have its interval set to at least 60000ms.
Setting alarm in Widgets onEnabled():
AlarmManager am= (AlarmManager)context.getSystemService(Context.ALARM_SERVICE);
Intent intent = new Intent(context, AlarmManagerBroadcastReceiver.class);
PendingIntent pi = PendingIntent.getBroadcast(context, 0, intent, 0);
//After after 3 seconds
am.setRepeating(AlarmManager.RTC_WAKEUP, System.currentTimeMillis()+ 3000, 1000 , pi);
then in the AlarmManagerBroadcastReceiver's onReceive():
PowerManager pm = (PowerManager) context.getSystemService(Context.POWER_SERVICE);
PowerManager.WakeLock wl = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "TAG");
//Acquire the lock
wl.acquire();
/*
* ......
* Update Widgets RemoteViews
*/
wl.release();
In the docs for setRepeating() it says:
Note: as of API 19, all repeating alarms are inexact. If your application needs precise delivery times then it must use one-time exact alarms, rescheduling each time as described above. Legacy applications whose targetSdkVersion is earlier than API 19 will continue to have all of their alarms, including repeating alarms, treated as exact.
it also now states:
Schedule a repeating alarm. Note: for timing operations (ticks, timeouts, etc) it is easier and much more efficient to use
Handler
So how would you go about updating the Widgets Remoteviews using a Handler? How would you get it to stop when the device is put to sleep to conserve battery?
Are there any other suggested ways to update a Widget?