When I call this.startService(new Intent(this, Some_Service.class));
I know that onCreate()
is called and followed by onStartCommand()
.
Bur what happens if the code in onStartCommand()
done?.
Does Service
automatically call onDestroy()
to stop itself?
Or do I must call this.stopService(new Intent(this, Some_Service.class));
to achieve that?
If you start a service by calling startService()
then you have to call stopService()
or stopSelf()
to stop the service. If you want to stop a service after doing some work, you might want to use IntentService
instead.
onStartCommand()
-
The system calls this method when another component, such as an activity, requests that the service be started, by calling startService()
. Once this method executes, the service is started and can run in the background indefinitely. If you implement this, it is your responsibility to stop the service when its work is done, by calling stopSelf()
or stopService()
. (If you only want to provide binding, you don't need to implement this method.)
Read Here