I am stuck with the problem of Activity + Service
in that I have following number of Activities and Services.
If the process that runs your service gets killed, the Android system will restart it automatically it is default behavior.
This behavior is defined by the return value of onStartCommand()
in your Service implementation. The constant START_NOT_STICKY
tells Android not to restart the service if it s running while the process is "killed".
You need to Override method onStartCommand()
in your service class and move all your code from onStart()
method to onStartCommand()
method.
According to the Android Documentation:
For started services, there are two additional major modes of operation they can decide to run in, depending on the value they return from
onStartCommand()
:START_STICKY
is used for services that are explicitly started and stopped as needed, whileSTART_NOT_STICKY
orSTART_REDELIVER_INTENT
are used for services that should only remain running while processing any commands sent to them
onStart()
method calls each time when service is restarted but onStartCommand()
method will not called if you return START_NON_STICKY
.
Don't use onStart() anymore, it's deprecated.
I hope it helps you.