Using AlarmManager to update widgets manually
As stated in the Android Dev Guide, if you want a widget to update more often you should use the AlarmManager
to set alarms that do not wake the device.
In principle: do not use the standard mechanisms provided by the AppWidgetProvider
class and its easy set up with android:updatePeriodMillis in the xml file.
I am sorry to ask, but the guide just states "use AlarmManager, use RTC
or ELAPSED_REALTIME
, ..." but HOW EXACTLY do I send an intent to just update my widget is missing!!!
Could anybody please state the code I need to form the PendingIntent
that mimics the default behaviour? I don't have a clue how to get the widget ids, which action I should use and so on... Sad the dev guide stops explaining at that point!
What is needed as extras to which action?
In case somebody is interested why I want to update more frequently than 30 minutes: my widget shows when the next bus departs at a station. There is a bus every 20 minutes, so I got two options: update the widget every minute, displaying the departure time of the next bus (that's what I want!!) or, not that good, state the departure time of the next bus so I will have to update at least every 20 minutes!
Now: when the device is asleep, of course, this should never wake it up - so my understanding of this section in the dev guide is that this is the correct way to implement it; anybody thinks I'm wrong? Oh, another thing I would like to find out: if the device is asleep when the widget should be updated and my alarm is not issued due to the sleeping device, will it be updated IMMEDIATELY when it wakes up???
Thanks for your help!!
PS: I really wonder why the xml definition of a widget provider does not allow to simply state "don't wake the device" by a boolean switch... that would make life so much easier in the first place! What kind of a widget needs to wake device anyhow??? ;-)
This is how far I came up to now, but it does not work - nothing hap开发者_开发百科pens:
private void startAlarm(Context pContext) {
Log.d(TAG, "startAlarm");
AlarmManager am = (AlarmManager) pContext.getSystemService(Context.ALARM_SERVICE);
Intent intent = new Intent("android.appwidget.action.APPWIDGET_UPDATE");
intent.setClass(pContext, getClass());
PendingIntent pi = PendingIntent.getBroadcast(pContext, 0, intent, PendingIntent.FLAG_CANCEL_CURRENT);
am.set(AlarmManager.ELAPSED_REALTIME, SystemClock.elapsedRealtime() + 1000, pi);
}
Could anybody please state the code I need to form the PendingIntent that mimics the default behaviour?
I wouldn't, because AFAIK it is not possible to "mimic the default behavior" precisely. Simply have the AlarmManager
alarm use a PendingIntent
that either sends a custom action to your AppWidgetProvider
that you process in onReceive()
, or calls startService()
on the IntentService
you are using for the real smarts behind your app widget.
I don't have a clue how to get the widget ids
You saved the widget IDs from actual onUpdate()
calls, such as when the app widget is first put on the screen. When your alarm goes off, update all your app widgets.
update the widget every minute, displaying the departure time of the next bus (that's what I want!!)
Please allow this to be user-configurable.
Oh, another thing I would like to find out: if the device is asleep when the widget should be updated and my alarm is not issued due to the sleeping device, will it be updated IMMEDIATELY when it wakes up???
AFAIK, yes, but I have not used alarms that are not of the _WAKEUP
variety.
Don't know if they can still help you but I wrote some posts about Android Widget some days ago.
Here is the one regarding the AlarmManager:
http://malubu.wordpress.com/2012/06/05/take-your-time-widgets-and-alarmmanager/
精彩评论