jibysthomas
jibysthomas

Reputation: 1621

Network call from Android widgets

How it is possible to do a network call from a widget in Android?
I have a widget which will show the result from an API call from a webservice.
So how it is possible to call the HttpGet method in a widget?

Upvotes: 1

Views: 1308

Answers (1)

CommonsWare
CommonsWare

Reputation: 1006614

So how it is possible to call the Httpget method in a widget?

An AppWidgetProvider is a subclass of BroadcastReceiver, and methods like onUpdate() are called from the receiver's onReceive(). It is not safe for you to do HTTP I/O here, as you are on the main application thread, and this work may take too long, freezing your UI and possibly causing an ANR dialog.

Instead, delegate the HTTP work to an IntentService you trigger from onUpdate(). The IntentService can then update the RemoteViews of the app widget, if needed.

Upvotes: 4

Related Questions