0

I've read the developers' guide about App Widgets and I've understood that to avoid ANR errors you should start a service within the onUpdate method of your AppWidgetProvider to update the widgets... I have a question about such a service: when we develop standard services, which can be started from an activity, we should avoid too long computations in onStartCommand, since the onStartCommand method runs in the main UI thread.. so, should we avoid long computations also within the onStartCommand of a Service started by the AppWidgetProvider or in this case we don't risk to block the UI? If we must avoid long computations in onStartCommand also in this case, could we start a worker thread within the service, or use an Intent Service? In this case can we update the widget with the updateAppWidget method of AppWidgetManager from whatever thread we want or do we risk to update the UI from outside the UI thread?

I hope the questions are quite clear..

Gianni Costanzi
  • 6,054
  • 11
  • 48
  • 74

1 Answers1

1

You can launch an Intent Service from the onUpdate and then update the widget from the service using AppWidgetManager as usual. This doesn't mean you are updating UI outside the UI thread. Even when you're updating a widget from onUpdate, you are using RemoteViews and AppWidgetManager, you aren't updating the UI directly. AppWidgetManager actually calls an internal Android service that updates the widget, so you don't need to worry about it.

I think that the UI thread for widgets is in the launcher, but I'm not sure. In any case, the widget's code doesn't run on that thread, that's why we have RemoteViews.

Ran
  • 4,117
  • 4
  • 44
  • 70
  • Thanks for the answer.. btw, if I don't use an Intent Service but a Service, and then I got stuck in the *onStartCommand* for some seconds, do I risk ANR errors also in this context? The documentation of *onStartCommand* recommends you to spawn another thread for long activities, which is not what they do in the [Wictionary example]( http://code.google.com/p/wiktionary-android/source/browse/trunk/Wiktionary/src/com/example/android/wiktionary/WordWidget.java) cited in the developer's guide.. – Gianni Costanzi Jun 11 '12 at 21:05
  • Any idea about the possible ANR caused by executing long waits within the *onStartCommand* method of the invoked Service? – Gianni Costanzi Jun 13 '12 at 14:29
  • 1
    Like the doc says, service code runs in the UI thread. You should either use IntentService and do you work in `onHandleIntent(Intent)` or use a regular service and launch your own thread. – Ran Jun 14 '12 at 14:42
  • 1
    So I've understood it correctly, but the example was not soo good... They said "do not perform long waits and computations in the widget provider *onUpdate* method, invoke a service instead" and then they show you an example that launches a service and executes potentially long computation within the UI thread.. they are a bit misleading.. btw, thanks – Gianni Costanzi Jun 14 '12 at 19:51