How can I prevent my Android app/service from being “killed” from a task manager?

后端 未结 6 448
你的背包
你的背包 2020-12-13 07:24

It is very important that my service stay running until someone with a password stops the service from my UI screen. My app runs great but it is designed to be turned on/off

6条回答
  •  盖世英雄少女心
    2020-12-13 07:50

    You can use API method: startForeground(). Here is the explanation of it:

    A started service can use the startForeground(int, Notification) API to put the service in a foreground state, where the system considers it to be something the user is actively aware of and thus not a candidate for killing when low on memory. (It is still theoretically possible for the service to be killed under extreme memory pressure from the current foreground application, but in practice this should not be a concern.)

    Here you can find an example how to use this.

    As for the question, you cannot prevent a service from being killed. It can be killed by the system. Even system services can be killed. If this happens they are restarted. You may use the same approach.

提交回复
热议问题