Service crashing and restarting

前端 未结 3 1326
無奈伤痛
無奈伤痛 2020-12-06 08:30

There are several questions about it but I always read the same thing: \"the service will be killed if the system need resources\" or \"you can\'t build an service that runs

3条回答
  •  感动是毒
    2020-12-06 08:56

    As the documentation says, a Service runs in the main thread of its callee, that usually is the UI Thread. So what is happening is that when you kill your application, you kill your application process and thus the service is killed too.

    You can workaround this behavior by creating your Service in a different process by using android:process in your tag in the Manifest.xml file.

    Usually, though, you start a Service in its own process if the Service needs to be independent from the callee and if it may be used by different application. If your Service is for your own application use only, then stick with the default behavior and simply don't kill you application.

    EDIT 1:

    The documentation for android:isolatedProcess says:

    If set to true, this service will run under a special process that is isolated from the rest of the system and has no permissions of its own. The only communication with it is through the Service API (binding and starting).

提交回复
热议问题