Does startService () create a new instance of the service or use an existing one?

Does startService () create a new instance of the service or use an existing one? For example, in the following code, are two instances of UpdateService or one instance of UpdateService created? Thank.

int[] appWidgetIds = new int[] {1, 2}; for (int appWidgetId : appWidgetIds) { Intent intent = new Intent(context, UpdateService.class); intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId); context.startService(intent); } 
+28
android
Mar 25 '10 at 18:14
source share
2 answers

If the service is already running, it does not start as a second copy, but onStart() is still called on the existing instance. Services are natural singleton - there is only 0 or 1 copy of a service in action.

+48
Mar 25 '10 at 19:24
source share

The onStart method is onStart deprecated, use onStartCommand .

+14
Feb 16 '11 at 3:13
source share



All Articles