Android Question What is the recommended way to stop a service within the service itself

JohnC

Expert
Licensed User
Longtime User
I have an app that has a feature that uses a service module. If the user enables this feature, it is enabled when the device is booted up using the "#StartAtBoot: True" in the services module

However, if the user has this feature disabled, I would like to not have the service running all the time.

So, I wanted to put some code in the service module that when the service is started, it will see if this feature is enabled, and if not, have the service close itself.

So in what service module event sub would I place "StopService" and should I using something other then StopService to accomplish this?
 

LucaMs

Expert
Licensed User
Longtime User
I think you should add StopService(Me) in Service_Start (depending on some data saved - KeyValueStore preferences), because if you write that line in Service_Create Service_Start will be executed anyway.

Note that if you have also the Starter Service, it will be executed anyway before your "start at boot" Service.
 
Upvote 0

JohnC

Expert
Licensed User
Longtime User
Yeah, but is it going to be 100% reliable to place the ServiceStop in the Service_start sub?

I ask because I've had problems doing something similar (putting a Activity.Finish in the Activity_Create sub)
 
Upvote 0

JohnC

Expert
Licensed User
Longtime User
Thanks. I'll give it a shot.
 
Upvote 0
Top