Issue
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 complete the long running task and come back to the widget if my app dies in the middle of the request?
Solution
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
.
Answered By - CommonsWare
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.