user849998
user849998

Reputation:

AppWidgetProvider(Widget) with Service Android

Why in almost all the tutorials or examples people do that:

@Override
public void 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?

Upvotes: 2

Views: 1326

Answers (1)

Femi
Femi

Reputation: 64710

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.

Upvotes: 2

Related Questions