Skip to content

How to kill currently running task in android

I am trying to build a task killer type of app in android. I can show the list of currently running task using ActivityManager but facing problem in killing the task.

Here is what i am doing to get the list of currently running task :

ActivityManager am = (ActivityManager) context.
            getSystemService(Activity.ACTIVITY_SERVICE);
List<RunningTaskInfo> a = am.getRunningTasks(Integer.MAX_VALUE);

PackageManager pack = this.getPackageManager();

for(int i = 0; i < a.size(); i++) {
    String packageName = a.get(i).topActivity.getPackageName();
    Drawable d = null;
    String appName = "";

    try {
        d = pack.getApplicationIcon(packageName);
        appName = (String)pack.getApplicationLabel(pack.getApplicationInfo(packageName,PackageManager.GET_META_DATA));
    } catch (NameNotFoundException e) {
        e.printStackTrace();
    }
    
    packName.add(packageName);  //arraylist of package name
    apps.add(appName);          // arraylist of app name
    icons.add(d);               //arraylist of icons
}

It worked for me But now as I am trying to kill the task using killBackgroundProcesses :

am.killBackgroundProcesses(package name);

It did not work . Also killBackgroundProcesses method is available in API level 2.2 then how can i perform the same task in previous API level such as 1.6 etc.

Is there any other way to show the currently active apps and kill them?

Answer

No one can kill process except Android OS itself.

Most of the task killer in android market don’t kill the app they just restart the process

by using

public void restartPackage (String packageName)

when this method is called by your activity the operating system immediately called

savedInstanceState and save the state of that activity you want to kill. Now this process is

removed from memory and OS saved it state.Now when next time user start that activity it

will start from where it was killed or in other words restarted. You can verify it from any

task manager that they don’t kill the process because no one can do so. This method also

work in ICS.

for above method you can look at here . As far as i know killBackgroundProcesses (String packageName) is for API 8 and above.