Android task killer

2019-01-18 00:52发布

Im trying to write a simple task killer. I know I shouldnt kill tasks in Android, but Im eager to try something like this. I have the following code:

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);
}

My problem here is that when I perform this code, I first get the list of all tasks, thats ok. Than I see in the log several lines of:

Sending signal. pid: (processId) SIG: 9

I asume thats a signal to die. But at the end of my code, when I display all running processes, the list is the same as before, no task has been killed. Any ide why? Thank you!

标签: android task
3条回答
叛逆
2楼-- · 2019-01-18 01:19

Fortunately, killProcess() will only work for you to kill your own processes. You can tell this by reading the documentation.

查看更多
小情绪 Triste *
3楼-- · 2019-01-18 01:23

You can't kill other tasks that way because the Kernel will enforce permissions. Try this instead:

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

you will need the following permissions in the manifest:

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

EDIT:

Actually restartPackage is deprecated. use killBackgroundProcesses() instead!

查看更多
对你真心纯属浪费
4楼-- · 2019-01-18 01:32

To achieve best result form killProcess , try calling killProcess many times.as

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

NOTE:

version 2.2 - killBackgroundProcesses : " This is the same as the kernel killing those processes to reclaim memory; the system will take care of restarting these processes in the future as needed."

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

Required Permissions:

android.Manifest.permission.KILL_BACKGROUND_PROCESSES

version 2.1 - restartPackage -" This method is deprecated. (works for version less than 2.2. This is now just a wrapper for killBackgroundProcesses(String); the previous behavior here is no longer available to applications because it allows them to break other applications by removing their alarms, stopping their services, etc."

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

Required Permissions:

<uses-permission android:name="android.permission.RESTART_PACKAGES" />
查看更多
登录 后发表回答