AppWidgetProvider(Widget) with Service Android
Why in almost all the tutorials or examples people do that:
@Override
public vo开发者_StackOverflow社区id onUpdate(Context context, AppWidgetManager appWidgetManager,
int[] appWidgetIds) {
...
context.startService(resumeIntent);
super.onUpdate(context, appWidgetManager, appWidgetIds);
}
Doesn't it mean that the new service is started every time the update is executed? Is it the best solution? Or is it better to do it with sending broadcasts? And shouldn't
context.startService(resumeIntent);
better be done in onEnabled method?
No, the service is started ONLY IF it isn't already running: if it is already running it is sent a new Intent in onStartCommand
and it can process that appropriately.
As far as whether to use this or to use onEnabled
the advantage this has is that the service is restarted every single time if it is not running: onEnabled
will start the service but if the service is killed for any reason you may not get it restarted for you.
精彩评论