I'm not having much luck with updating an app widget with AlarmManager
generated broadcasts. Here's what I do:
Initializing AlarmManager
on AppWidgetProvider#onEnabled
AlarmManager alarms = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
alarms.setRepeating(AlarmManager.ELAPSED_REALTIME_WAKEUP,
SystemClock.elapsedRealtime(), 60000, pendingIntent);
I also define BroadcastReceiver
that simply listens for the updates that fired by the AlarmManager
. When update is fired code runs AsyncTask
that makes a network call. When the AsyncTask
is completed (onPostExecute
) it uses previously obtained instance of AppWidgetManager
to update the widget(s).
It all actually runs well until in the logs I see message after which the AlarmManager
never fires another update:
Process com.foo.myapp (pid 12345) has died
Do I need to have some sort of check which will restart the alarms? For example when user access the parent app of the widget? How do I ensure that I can co开发者_运维知识库mplete the long running task and come back to the widget if my app dies in the middle of the request?
When update is fired code runs AsyncTask that makes a network call.
If this is inside the BroadcastReceiver
, that won't work. You cannot safely fork threads from a BroadcastReceiver
, and AsyncTask
effectively forks a thread to do its task asynchronously.
Instead, you should delegate long-running work to a service started from the alarm BroadcastReceiver
.
精彩评论