What Happens If a Android Service Is Started Multiple Times

What happens if a Android Service is started multiple times?

The Service will only run in one instance. However, everytime you start the service, the onStartCommand() method is called.

This is documented here

Calling startService() multiple times on a running service

If I start a Service 3 times while it is already running, do I have to call stopService 3 times to make it stop?

No. One stopService() is sufficient.

I know it's not a good practice to do so

Sure it is. If desired, your activity (or whatever is calling startService()) can blindly call startService() without knowing or caring whether the service is up and running. For example, if you are implementing an IntentService for handling downloads of mid-sized files, the activity can call startService() for as many downloads as the user wants. Note though that IntentService stops itself when it has no more work to do.

Android Service - startService is called multiple times and causes value mixup...

What is your return type in onStartCommand ?

You should read about the life cycle of the service.
https://developer.android.com/reference/android/app/Service.html#ServiceLifecycle

I would suggest to use IntentService, as it is designed for handling asyncron tasks, it also start in a worker thread.
http://developer.android.com/reference/android/app/IntentService.html

To really help you, the code of your service is quite important :)

start service multiple times with different intents

You don't run them in Parallel. The Android API handles this for you. If the service isn't running the first time you call startService(), then its onCreate() method is called. However, if it is running, then that results in a call to the service's onStartCommand().

Multiple requests to start the service result in multiple corresponding calls to the service's onStartCommand().

It's up to you to handle the logic to create a thread for each request to run that service. Take a look at this overview of how to accomplish your task:

http://developer.android.com/intl/es/guide/components/services.html#ExtendingService

That will walk you through creating your service correctly, allowing you to send multiple requests simultaneously.

context.startForegroundService(startServiceIntent) multiple time will call onCreate multiple times?

It depends if the service is running or not. startForegroundService() will trigger a call to onCreate() if that service is not already running.

So, for example:

  • You call startForegroundService()
  • Android creates an instance of your service and calls onCreate() on it
  • Android then calls onStartCommand() on it (which may trigger calls to other things, like onHandleIntent() of IntentService)
  • You call startForegroundService() again
  • Android realizes that you have a running copy of the service and does not create a new one, so there is no onCreate() call
  • Android then calls onStartCommand() on it (which may trigger calls to other things, like onHandleIntent() of IntentService)
  • You stop your service, via something like stopService() or stopSelf() (or onHandleIntent() returns, if you are still using IntentService)
  • You call startForegroundService() again, because you really like that method
  • Android creates an instance of your service and calls onCreate() on it
  • Android then calls onStartCommand() on it (which may trigger calls to other things, like onHandleIntent() of IntentService)

Android Service starting 2 times because of BindService and StartService

You don't have an unbindService call anywhere in your code. So whenever the Activity gets destroyed, the system detects that it is still bound to a ServiceConnection and has been leaked. This is still the case when calling bindService inside of a Fragment. Since fragments don't inherit from Activity or Context, they don't have a context reference themselves thus they must use their parent Activities context. Remember to always call unbindService when the owning component is being destroyed, whether it's a Fragment, Activity, or even another Service. It's not unheard for a service to bind to another.

If you don't want your bound service to be destroyed when all clients unbind, you need to add special logic to determine if the Service should transition to a started service temporarily so it won't be killed by the OS, and stop the service when a client rebinds to it.

How to make sure that you don't start a service twice android

fix it with a boolean/flag in the service.
(A service can only be started once)

Prevent multiple copies of an Android service

The problem was I had declared my myService class to extend IntentService, not Service!
Once I fixed that, it all worked as per the book!



Related Topics



Leave a reply



Submit