How do Task Managers kill apps?

三世轮回 提交于 2019-11-26 08:24:57

问题


First of all I know it\'s bad to use a task manager/killer in Android and all that, but what I was wondering is how do task managers like Advanced Task Killer kill other applications?
I wanted to develop a simple application that would do this, just for the learning experience.
I tried executing the Linux command kill pid from my application but it didn\'t work, maybe it requires root?

So, how do I accomplish this from my application? I have a simple ListActivity that shows the currently running tasks and when a user long-presses an item I want to kill that task.


回答1:


You can send the signal using:

Process.sendSignal(pid, Process.SIGNAL_KILL);

To completely kill the process, it's recommended to call:

ActivityManager.killBackgroundProcesses(PackageName)

before sending the signal.




回答2:


slayton has good answer in this question.I add this detail to his answer:
- when you use ActivityManager.killBackgroundProcesses(PackageName) , you can not kill foreground process.

I saw these open sources project link in K_Anas'answer to this question:
- github repository
- code.google




回答3:


try this,

android.os.Process.killProcess(pid)

that will work...




回答4:


1- Add to manifest

<uses-permission android:name="android.permission.KILL_BACKGROUND_PROCESSES"/>

2 - In your code

Runtime.getRuntime().exec("adb shell killall com.example.app");

Note : Your app needs to have access to adb shell system/app (root permission)



来源:https://stackoverflow.com/questions/6303615/how-do-task-managers-kill-apps

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!