如何防止我的 Android 应用程序/服务被任务管理器“杀死”?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2785843/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How can I prevent my Android app/service from being "killed" from a task manager?
提问by Don
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
非常重要的是,我的服务保持运行,直到有人通过密码从我的 UI 屏幕停止服务。我的应用程序运行良好,但它的设计目的是由父母(使用密码)在他们的孩子手机上打开/关闭。我设法使一切正常,但我遇到的问题是,如果孩子使用任务管理器来终止我的服务,那么我的应用程序就毫无用处。我将感谢任何知道方法的人
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?
1) 监视服务并在它“被杀死”时自动启动它,或者 2) 防止有人能够杀死它,除了启动该服务的活动(管理屏幕)。或两者?
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.
如果我在描述问题时不是很清楚,我很抱歉,我是初学者。到目前为止,我已经取得了很大的进步,但我被困在了最后一个障碍上。
回答by Yury
You can use API method: startForeground()
. Here is the explanation of it:
您可以使用 API 方法:startForeground()
. 这是它的解释:
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.)
一个已启动的服务可以使用 startForeground(int, Notification) API 将服务置于前台状态,系统认为它是用户主动意识到的东西,因此不会在内存不足时被杀死。(在当前前台应用程序的极端内存压力下,服务在理论上仍然可能被终止,但实际上这不应该是一个问题。)
Hereyou 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.
至于问题,您无法阻止服务被杀死。它可以被系统杀死。甚至系统服务也可以被杀死。如果发生这种情况,它们将重新启动。您可以使用相同的方法。
回答by Tinfu
You can write a helper app to receive android broadcast "android.intent.action.PACKAGE_RESTARTED",when your app got killed,your helper will receive that broadcast and you can restart your app or whatever.
您可以编写一个助手应用程序来接收 android 广播“android.intent.action.PACKAGE_RESTARTED”,当您的应用程序被杀死时,您的助手将收到该广播,您可以重新启动您的应用程序或其他任何内容。
That's how 'Smart App Protector Free' do.
“Smart App Protector Free”就是这样做的。
The bad thing is users must install two apps instead of one.
不好的是用户必须安装两个应用程序而不是一个。
回答by Yann Ramin
There isn't a way to prevent this directly, without a rooted device. The SDK helpfully prevents these kinds of issues.
没有根设备,没有办法直接防止这种情况。SDK 有助于防止此类问题。
You can do the "truly evil" trick and have two services in two application. Each service monitors the other, and restarts it if it stops. This is kludgy, but in most cases even the fastest fingered kid couldn't kill both applications.
您可以使用“真正的邪恶”技巧并在两个应用程序中使用两个服务。每个服务都监视另一个服务,如果停止,则重新启动它。这很笨拙,但在大多数情况下,即使是手指最快的孩子也无法杀死这两个应用程序。
回答by Matthew Fisher
For anyone who is still searching for an answer - this one may be correct:
对于仍在寻找答案的任何人 - 这个答案可能是正确的:
you can not: make a service unkillable, if running on low Memory the System will always kill your service. BUT
您不能:使服务不可终止,如果在低内存上运行,系统将始终终止您的服务。但
you can: Tell the System to restart your service when it is killed. Look at this piece of code:
你可以:告诉系统在它被杀死时重新启动你的服务。看这段代码:
public static final int START_REDELIVER_INTENT
公共静态最终 int START_REDELIVER_INTENT
Added in API level 5
在 API 级别 5 中添加
Constant to return from onStartCommand(Intent, int, int)
:
从 返回的常量onStartCommand(Intent, int, int)
:
if this service's process is killed while it is started (after returning from onStartCommand(Intent, int, int))
, then it will be scheduled for a restart and the last delivered Intent re-delivered to it again via onStartCommand(Intent, int, int)
.
This Intent will remain scheduled for redelivery until the service calls stopSelf(int)
with the start ID provided to onStartCommand(Intent, int, int)
. The service will not receive an onStartCommand(Intent, int, int)
call with a null Intent because it will only be re-started if it is not finished processing all Intents sent to it (and any such pending events will be delivered at the point of restart).
如果此服务的进程在启动时被终止(从 返回后onStartCommand(Intent, int, int))
,它将被安排重新启动,并且最后一次交付的 Intent 通过 再次重新交付给它onStartCommand(Intent, int, int)
。此 Intent 将保持重新交付的安排,直到服务调用stopSelf(int)
start ID 提供给onStartCommand(Intent, int, int)
。该服务将不会收到onStartCommand(Intent, int, int)
带有空 Intent的调用,因为只有当它没有完成处理发送给它的所有 Intent 时才会重新启动它(并且任何此类挂起的事件都将在重新启动时传递)。
Constant Value: 3 (0x00000003)
常数值: 3 (0x00000003)
回答by JoshuaTree
If you have system level permissions use persistent:true
via manifest permissions.
如果您具有系统级权限,请使用persistent:true
清单权限。
https://developer.android.com/guide/topics/manifest/application-element
https://developer.android.com/guide/topics/manifest/application-element
回答by prateek gupta
Just set the return type as START_STICKY .
只需将返回类型设置为 START_STICKY 。