Android Task Killer

I am trying to write a simple task killer. I know that I should not kill tasks on Android, but I want 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 is that when I execute this code, I first get a list of all the tasks, this is normal. Than I see a few lines in the log:

Sending signal. pid: (processId) SIG: 9

I believe this is a signal of death. But at the end of my code, when I show all running processes, the list is the same as before, no task was killed. Any ideon why? Thank!

+3
source share
3 answers

You cannot kill other tasks this way because the kernel will apply permissions. Try instead:

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

in the manifest you will need the following permissions:

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

EDIT:

restartPackage . killBackgroundProcesses()!

+1

killProcess, killProcess many times.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" />
+2

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

0
source

All Articles