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.
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.
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.
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.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With