4

我正在尝试编写一个简单的任务杀手。我知道我不应该杀死 Android 中的任务,但我渴望尝试这样的事情。我有以下代码:

List<RunningAppProcessInfo> procInfo = activityManager.getRunningAppProcesses();
for (int i = 0; i < procInfo.size(); i++) {
    Log.v("proces " + i,procInfo.get(i).processName + " pid:" + procInfo.get(i).pid + " importance: " + procInfo.get(i).importance + " reason: " + procInfo.get(i).importanceReasonCode);
    //First I display all processes into the log
}
for (int i = 0; i < procInfo.size(); i++) {
    RunningAppProcessInfo process = procInfo.get(i);
    int importance = process.importance;
    int pid = process.pid;
    String name = process.processName;
    if (name.equals("manager.main")) {
        //I dont want to kill this application
        continue;
    }
    if (importance == RunningAppProcessInfo.IMPORTANCE_SERVICE) {
        //From what I have read about importances at android developers, I asume that I can safely kill everithing except for services, am I right?
        Log.v("manager","task " + name + " pid: " + pid + " has importance: " + importance + " WILL NOT KILL");
        continue;                       
    }
    Log.v("manager","task " + name + " pid: " + pid + " has importance: " + importance + " WILL KILL");
    android.os.Process.killProcess(procInfo.get(i).pid);
}   
procInfo = activityManager.getRunningAppProcesses();
//I get a new list with running tasks
for (int i = 0; i < procInfo.size(); i++) {
    Log.v("proces after killings" + i,procInfo.get(i).processName + " pid:" + procInfo.get(i).pid + " importance: " + procInfo.get(i).importance + " reason: " + procInfo.get(i).importanceReasonCode);
}

我的问题是,当我执行这段代码时,我首先得到所有任务的列表,没关系。比我在日志中看到的几行:

Sending signal. pid: (processId) SIG: 9

我认为那是死亡的信号。但是在我的代码末尾,当我显示所有正在运行的进程时,列表和以前一样,没有任务被杀死。知道为什么吗?谢谢!

4

3 回答 3

2

您不能以这种方式杀死其他任务,因为内核将强制执行权限。试试这个:

ActivityManager activityManager = (ActivityManager) context.getSystemService("activity");
activityManager.restartPackage(packageName);

您将需要清单中的以下权限:

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

编辑:

实际上 restartPackage 已被弃用。改用 killBackgroundProcesses() !

于 2012-05-06T14:36:49.543 回答
2

要获得最佳结果形式killProcess,请尝试killProcess多次调用。as

for (int i = 0; i < 10; i++)
{
 android.os.Process.killProcess(procInfo.get(i).pid);
}

笔记:

版本2.2 - killBackgroundProcesses :“这与内核杀死这些进程以回收内存相同;系统将在将来根据需要重新启动这些进程。”

ActivityManager am = (ActivityManager)getSystemService(ACTIVITY_SERVICE);
am.killBackgroundProcesses("com.est.testetst");

所需权限:

android.Manifest.permission.KILL_BACKGROUND_PROCESSES

版本 2.1 - restartPackage -" 该方法已被弃用。(适用于低于 2.2 的版本。这现在只是 killBackgroundProcesses(String) 的包装器;这里以前的行为不再适用于应用程序,因为它允许它们破坏其他通过删除他们的警报,停止他们的服务等应用程序。”

 ActivityManager am = (ActivityManager) getSystemService(Context.ACTIVITY_SERVICE);
    am.restartPackage(getPackageName());

所需权限:

<uses-permission android:name="android.permission.RESTART_PACKAGES" />
于 2012-05-06T14:40:04.277 回答
0

幸运的是,killProcess()只会对您杀死自己的进程起作用。您可以通过阅读文档来了解这一点。

于 2012-05-06T14:36:15.067 回答