Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Starting Android service already running?

Sure this is a trivial question. What happens if I start a Service, using the following code:

 startService(new Intent(this,myService.class)); 

and then I accidentally recall the above code, while the Service is yet running?

I'm afraid that the second call to startservice can create a new Service in order to have two different process executing at same time.

like image 929
GVillani82 Avatar asked Oct 29 '12 14:10

GVillani82


People also ask

What happens if we start a service twice android?

Multiple requests to start the service result in multiple corresponding calls to the service's onStartCommand(). However, only one request to stop the service (with stopSelf() or stopService()) is required to stop it.

How do you check if a service is already running Android?

You can do this by making your own Interface where you declare for example " isServiceRunning() ". You can then bind your Activity to your Service, run the method isServiceRunning(), the Service will check for itself if it is running or not and returns a boolean to your Activity.

Can we start service multiple times in Android?

Absolutely Correct. Only one instance of Service is created for an application process. And when you call StartService(); again, then only onStartCommand() gets called and new Intent is passed to onStartCommand() method. Note: onCreate() is not called again.

How do I trigger a service in Android?

Start a service. An Android component (service, receiver, activity) can trigger the execution of a service via the startService(intent) method. // use this to start and trigger a service Intent i= new Intent(context, MyService. class); // potentially add data to the intent i.


1 Answers

I'm afraid that that the second call to startservice can create a new service in order to have two different process executing at same time.

No, on multiple counts:

  • No, it will not create a new service. If the service is already running, it will be called with onStartCommand() again, to deliver the new Intent, but a second copy is not created.

  • No, it will not "have two different process executing at same time", because a service runs in the same process as the rest of your app, by default.

like image 191
CommonsWare Avatar answered Sep 17 '22 17:09

CommonsWare