任务杀手如何运作?
任务杀手
应用程序的实用性存在争议,但我想知道:它们实际上是如何工作的?如何杀死特定进程?
是否有用于此目的的 API?如果有,它实际上做什么?
编辑
值得补充:我看到任务杀手应用程序杀死了未root设备
上的进程。所以,我想知道如何杀死 Android 中不属于您的进程?
The usefullness of task killer
apps is debated, but I'm wondering: how do they actually work? How is it possible to kill particular process?
Is there an API for this, and if so what does it actually do?
EDIT
Worth adding: I saw task killer apps kill processes on not rooted devices
. So, I wonder how is it possible to kill process, which you don't own in Android?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
简而言之,自动任务杀手的工作原理是轮询操作系统以获取当前正在运行的进程及其消耗的内存列表。然后,任务杀手通过智能算法或用户输入向系统发出调用,告诉系统终止进程。有两个 api 可以做到这一点。
它们是
Process.killProcess(int pid)
ActivityManager.killBackgroundProcesses(String packageName)
首先通过调用
Process.killProcess(int pid)
来实现,其中pid
是特定进程的唯一标识符。 Android 终止进程的方式与 Linux 相同;但是,用户只能终止他们拥有的进程。在 Android 中,每个应用程序都使用唯一的 UID (UserID) 运行。使用此 API 的应用程序只能终止自己的进程,因此以下 中的解释Process.killProcess(int pid)
的文档:当调用此方法时,操作系统会生成信号并将其发送到进程。每当进程从操作系统接收到信号时,它必须处理该信号或立即死亡。诸如
SIG_KILL
之类的信号无法被处理并导致接收进程立即死亡。如果您想终止您无权终止的进程,即它不是您的进程,那么您必须切换用户或升级您的权限(在 Android 上,这需要设备上的 root 权限)。第二个 API 的工作原理是告诉内置的 ActivityManager 您想要终止与特定包关联的进程。 此 API 不需要您的 UID 与进程的 UID 相匹配,因为它要求用户接受
KILL_BACKGROUND_PROCESSES
权限。此权限向操作系统发出信号,表明用户已批准应用程序作为任务杀手。当任务杀手想要终止某个应用程序时,它会告诉操作系统终止该进程,从而使应用程序能够解决只能终止其拥有的进程的问题。在 Android 文档中,它说这个 API 实际上使用第一个 <代码>Process.killProcess API
如果您想了解更多信息,我建议您阅读 Posix Signals 和 Linux 终止命令
In a nutshell, Automatic Task Killers work by polling the OS for a list of currently running processes and the memory they are consuming. Then either with an intelligent algorithm or with user input the Task Killers issue a call to the system telling the system to kill the process. There are two apis you can do this.
They are
Process.killProcess(int pid)
ActivityManager.killBackgroundProcesses(String packageName)
This first works by invoking
Process.killProcess(int pid)
wherepid
is the unique identifier for a specific process. Android kills processes in the same way that linux does; however, a user may only kill processes that they own. In Android each app is run with a unique UID (UserID). Apps using this API an App can only kill their own processes, hence the following explanation in the docs forProcess.killProcess(int pid)
:When this method is called the signal is generated by the OS and sent to the process. Whenever a process receives a signal from the OS it must either handle that signal or immediately die. Signals such as
SIG_KILL
cannot be handled and result in the immediate death of the recipient process. If you want to kill processes that you don't have privileges to kill, i.e. its not your process, then you must switch users or escalate your privileges (on android this requires root privileges on the device).The second API works by telling the built in
ActivityManager
that you wan to kill processes associated with a specific Package. This API gets around the need for your UID to match the UID of the process because it requires the user to accept theKILL_BACKGROUND_PROCESSES
permission. This permission signals to the OS that an app has been approved by the user as a task killer. When a task killer wants to kill an app, it tells the OS to kill the process allowing an app to get around the problem of only being able to kill processes that it owns.In the Android Docs it says that this API actually uses the first
Process.killProcess
APIIf you want to know more I suggest you read about the Posix Signals and The Linux kill command