I have a few applications using a shared library that starts a service when the user opens one of the apps. Ideally only one instance of this service will be running regardless of how many of these apps are open.
For example, the user opens app "A" which starts the service. Then the user opens app "B" which detects that the service is running, stops it, and then restarts the service itself.
Is this something that can be accomplished in Android by running the service in it's own process, or by some other method?
If:
Intent to identify the servicethen stopService() should work, assuming that the service can be stopped (e.g., there are no bound connections to it). stopService() is not limited to services in your own process.
The key here is security, ensuring that somehow you can stop the service and others cannot. That's not particularly easy, which is why you don't see this as a common pattern.
In your case, perhaps a better solution is not to stop the existing service and start a fresh one, but just to leave things alone, if your service instance is already running.
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