如何防止我的 Android 应用程序/服务被“杀死"?从任务管理器? [英] How can I prevent my Android app/service from being "killed" from a task manager?

查看:33
本文介绍了如何防止我的 Android 应用程序/服务被“杀死"?从任务管理器?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

让我的服务保持运行状态非常重要,直到有人知道密码从我的 UI 屏幕停止服务.我的应用程序运行良好,但它的设计目的是由父母(使用密码)在他们的孩子手机上打开/关闭.我已经设法使一切正常,但我遇到的问题是,如果孩子使用任务管理器来终止我的服务,那么我的应用程序将毫无用处.我将不胜感激任何知道方法的人

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 by parents (with a password) on their kids phones. I have managed to make everything work but the problem I'm having is that if the kid uses a task manager to kill my service then my app is useless. I would be grateful to anyone who knows a way to either

1) 监控服务并在它被杀死"时自动启动它或者2) 防止除启动该服务的活动(管理屏幕)之外的其他人能够杀死它.还是两者兼而有之?

1) monitor the service and start it back up automatically if its "killed" or 2) prevent someone from being able to kill it except from the activity (administration screen) that launched the service. Or both?

如果我在描述问题时不是很清楚,我很抱歉,我是初学者.到目前为止,我已经取得了很大的进步,但我被困在了最后一个障碍上.

I'm sorry if I'm not very clear in describing the problem, I'm a beginner. I've made great progress so far but I am stuck at this last hurdle.

推荐答案

您可以使用 API 方法:startForeground().解释如下:

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

启动的服务可以使用 startForeground(int, Notification) API将服务置于前台状态,系统认为它是用户主动意识到的东西,因此不是内存不足时杀戮的候选者.(理论上还是可能在极端内存压力下终止服务从当前的前台应用程序,但实际上这应该不用担心.)

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.

这篇关于如何防止我的 Android 应用程序/服务被“杀死"?从任务管理器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆