Service Automatic Called on Destroying Activity

后端 未结 4 929
天涯浪人
天涯浪人 2020-12-16 15:17

I am stuck with the problem of Activity + Service in that I have following number of Activities and Services.

Activities:

4条回答
  •  野趣味
    野趣味 (楼主)
    2020-12-16 16:16

    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, while START_NOT_STICKY or START_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.

提交回复
热议问题