Can I call stopSelf() in Service.onStartCommand?

AndroidAndroid Service

Android Problem Overview


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.

Android Solutions


Solution 1 - Android

> 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.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionSpencer RuportView Question on Stackoverflow
Solution 1 - AndroidCommonsWareView Answer on Stackoverflow