Question

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 broadcast receiver 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 "Process com.foo.myapp (pid 12345) has died" after which the AlarmManager never fires another update.

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?

Was it helpful?

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top