Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can I call stopSelf() in Service.onStartCommand?

There are some conditions where my service could be attempted to be started when it should not be. In cases like this is it bad form to call stopSelf() while inside a onStartCommand() method? If so what is the best way to handle such a situation? Any resources will be greatly appreciated.

like image 688
Spencer Ruport Avatar asked Nov 26 '11 14:11

Spencer Ruport


People also ask

What is stopSelf service?

stopSelf() is used to always stop the current service. stopSelf(int startId) is also used to stop the current service, but only if startId was the ID specified the last time the service was started. stopService(Intent service) is used to stop services, but from outside the service to be stopped.

Does stopSelf call onDestroy?

You should call stopSelf () to stop a service. After you call it, the Android Framework will call onDestroy() method automatically.


1 Answers

is it bad form to call stopSelf() while inside a onStartCommand() method?

Off the top of my head, I can't think of why that would be a problem.

stopSelf(), like a lot of stuff in Android, has no immediate effect. It puts a message on the message queue processed by the main application thread. The actual work of stopping the service will not even begin until sometime after onStartCommand() has returned.

like image 149
CommonsWare Avatar answered Oct 20 '22 18:10

CommonsWare