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?
I’m developing a Android AppWidget for my application. For updating my appwidget I need
Share
Delegate the download work to a service, probably an
IntentService, possibly aWakefulIntentServicedepending on whether there is a risk that the device might fall asleep during the work itself.Your
AppWidgetProviderwould just callstartService()on yourIntentService.Your
IntentService'sonHandleIntent()method would do the work you presently have inonUpdate(), getting its ownAppWidgetManagervia the staticgetInstance()method. But, sinceonHandleIntent()is executed on a background thread, you can take as much time as you need. TheIntentServicewill also automatically shut down once it is done with all outstanding work requests.