如何防止我的 Android 应用程序/服务被“杀死”?来自任务管理器?
非常重要的是,我的服务必须保持运行,直到有密码的人从我的 UI 屏幕停止该服务。我的应用程序运行良好,但它设计为由父母(使用密码)在孩子的手机上打开/关闭。我已经设法使一切正常工作,但我遇到的问题是,如果孩子使用任务管理器来终止我的服务,那么我的应用程序就没用了。我将不胜感激任何知道一种方法的人:
1)监视服务并在其“被杀死”时自动启动它 或者 2) 防止其他人能够通过启动该服务的活动(管理屏幕)之外的方式杀死它。或者两者都有?
如果我描述的问题不是很清楚,我很抱歉,我是初学者。到目前为止,我已经取得了很大的进步,但我陷入了最后的障碍。
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) 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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
您可以使用API方法:
startForeground()
。这是它的解释:此处您可以找到如何使用它的示例。
至于问题,你无法阻止服务被杀死。可以被系统杀死。甚至系统服务也可以被杀死。如果发生这种情况,它们将重新启动。您可以使用相同的方法。
You can use API method:
startForeground()
. Here is the explanation of it: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.
如果没有 root 设备,就没有办法直接阻止这种情况。 SDK 有助于防止此类问题。
您可以使用“真正邪恶”的技巧,在两个应用程序中拥有两项服务。每个服务都会监视另一个服务,并在停止时重新启动它。这很笨拙,但在大多数情况下,即使是手指最快的孩子也无法杀死这两个应用程序。
There isn't a way to prevent this directly, without a rooted device. The SDK helpfully prevents these kinds of issues.
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.
您可以编写一个辅助应用程序来接收 Android 广播 "android.intent.action。 PACKAGE_RESTARTED”,当您的应用程序被终止时,您的助手将收到该广播,您可以重新启动您的应用程序或其他任何内容。
这就是“Smart App Protector Free”的作用。
不好的是用户必须安装两个应用程序而不是一个。
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.
That's how 'Smart App Protector Free' do.
The bad thing is users must install two apps instead of one.
对于仍在寻找答案的任何人 - 这可能是正确的:
您不能:使服务不可杀死,如果在低内存上运行,系统将始终杀死您的服务。但是
您可以: 告诉系统在服务被终止时重新启动您的服务。看一下这段代码:
public static final int START_REDELIVER_INTENT
在 API 级别 5 中添加了
从
onStartCommand(Intent, int, int)
返回的常量:如果该服务的进程在启动时被终止(返回后)来自
onStartCommand(Intent, int, int))
,然后它将被安排重新启动,并通过onStartCommand(Intent, int, int)<再次将上次传递的Intent重新传递给它/代码>。
此 Intent 将保持重新传递的计划,直到服务调用
stopSelf(int)
并将开始 ID 提供给onStartCommand(Intent, int, int)
。该服务不会接收带有 null Intent 的onStartCommand(Intent, int, int)
调用,因为只有在未完成处理发送给它的所有 Intent(以及任何此类挂起的 Intent)时,它才会重新启动。事件将在重新启动时传递)。常量值:
3 (0x00000003)
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
Added in API level 5
Constant to return from
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 viaonStartCommand(Intent, int, int)
.This Intent will remain scheduled for redelivery until the service calls
stopSelf(int)
with the start ID provided toonStartCommand(Intent, int, int)
. The service will not receive anonStartCommand(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).Constant Value:
3 (0x00000003)
如果您拥有系统级权限,请通过清单权限使用
persistent:true
。https://developer.android.com/guide/topics/manifest/application-元素
If you have system level permissions use
persistent:true
via manifest permissions.https://developer.android.com/guide/topics/manifest/application-element
只需将返回类型设置为 START_STICKY 即可。
Just set the return type as START_STICKY .