Is it possible to call stopSelf () from Android's onStartCommand () service?

I have a service that, when called, checks some state values ​​and depending on the state it may be necessary to call some things asynchronously (locationmanager + asynctask) or just save the value and exit.

I am creating a service, not an intenservice because as I mention I need to create asynchronous tasks sometimes.

I read Services and I realized that I should call stopSelf when done. But in the case where I don't need to do anything, I do it when I exit onStartCommand.

So, would it work to just call stopSelf on the last line of onStartCommand if I ended up at that point? Are there any problems with this approach?

Thanks for the input.

+3


source to share


1 answer


If you are just performing if

a service check, why not perform this check before starting the service, thereby refraining from consuming valuable resources?



To answer your question - yes, it stopSelf()

will stop Service

, but don't start it if the service is not going to be used.

+1


source







All Articles