Android: network operations in Appwidget

I am developing an Android AppWidget for my application. To update my application, I need to download data from the network. But when I try to do this in the onUpdate method, my appWidget is killed by the Android system. I found out that the execution time of the operations cannot be performed in the receivers, so I started the service from the onUpdate method and started the thread from this. But the problem still persists. Can someone please let me know how to perform network operations in Appwidget?

+3
source share
1 answer

Pass download service work may IntentServicepossibly WakefulIntentService, depending on whether there is a risk that the device could fall asleep during the work itself.

Yours AppWidgetProviderwill just call startService()on yours IntentService.

Your method IntentService's onHandleIntent()will do the work you are currently using in onUpdate(), getting your own AppWidgetManagerusing the static method getInstance(). But, since it onHandleIntent()runs in the background thread, you can take as much time as you need. IntentServicewill also turn off automatically as soon as this is done with all outstanding work requests.

+3
source

Source: https://habr.com/ru/post/1760954/


All Articles