Android : Network operations in Appwidget
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?
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.
精彩评论