I'm developing a Android AppWidget for my application. For updating my appwidget I need to download data from network. But when I try to do that in onUpdate method, my appWidget is killed by Android system. I learnt that time consuming operations can't be done in receivers so I launched a service from onUpdate method and launched a thread from that. But still the problem persists. Can anyone please let me know how to do network operations in Appwidget?
Asked
Active
Viewed 1,461 times
1 Answers
3
Delegate the download work to a service, probably an IntentService
, possibly a WakefulIntentService
depending on whether there is a risk that the device might fall asleep during the work itself.
Your AppWidgetProvider
would just call startService()
on your IntentService
.
Your IntentService's
onHandleIntent()
method would do the work you presently have in onUpdate()
, getting its own AppWidgetManager
via the static getInstance()
method. But, since onHandleIntent()
is executed on a background thread, you can take as much time as you need. The IntentService
will also automatically shut down once it is done with all outstanding work requests.

CommonsWare
- 986,068
- 189
- 2,389
- 2,491
-
but how to combine that with use of RemoteViewsService? Should mentioned IntentService start RemoteViewsService after async operation is done? – Malachiasz Feb 05 '14 at 15:47
-
@Malachiasz: "Should mentioned IntentService start RemoteViewsService after async operation is done?" -- no. `RemoteViewsService` will be automatically used by the app widget framework. You need to make sure that the `RemoteViewsService` is aware of whatever data changes were made by the `IntentService`, such as by the `IntentService` updating a database that the `RemoteViewsService` needs to pull from. There should be a way to get the app widget to then reload its views from the `RemoteViewsService`, though I have not researched this and do not know off the top of my head how it is done. – CommonsWare Feb 05 '14 at 16:00