19

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?

Aniruddh Parihar
  • 3,072
  • 3
  • 21
  • 39
Dinesh Sharma
  • 11,533
  • 7
  • 42
  • 60
  • if(( this.getIntent()!= null)) { Boolean isLogout=this.getIntent().getBooleanExtra("close", false); if(isLogout) //finish(); android.os.Process.killProcess(android.os.Process.myPid()); } – Android Jan 11 '12 at 05:52
  • I have to kill the task not the process . It can be one task or all task that are currently running . – Dinesh Sharma Jan 11 '12 at 05:58
  • did not got any proper reply even after waiting for more than 30 hours ...... :( – Dinesh Sharma Jan 12 '12 at 10:12

3 Answers3

23

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.

Sunny
  • 14,522
  • 15
  • 84
  • 129
  • 1
    Thanks john for the valuable information but can you give some more information as I can show the list of currently running task (Not process) but now what to do to kill them as many apps are doing so ( ex. Advance task killer) and also I have tried **restartPackage (String packageName) and killBackgroundProcesses (String packageName)** , but when I perform this operation and then again try to find the currently running task still it shows the same number of task as earlier ...... so what should be the solution of this problem. Please share if you have some idea over this. thanks – Dinesh Sharma Jan 13 '12 at 11:32
  • 2
    Here task managers play a trick they just don't kill the app they just remove the item from the listview and that's app gone!!! i just made a task manager and it works like others.Also it free the internal memory (RAM) . don't forget to call `restartPackage(String packageNname)`before deleting that item. – Sunny Jan 13 '12 at 11:58
  • ok I will try the above trick and let you know about the result . thanks again... :) – Dinesh Sharma Jan 13 '12 at 12:15
  • hi john,I am facing one problem . The problem is that **restartPackage()** is working fine in android 1.6 but its not working in android 2.2 , can u tell me why this is happening . how can i make my app work in almost all android os version higher than 1.6. – Dinesh Sharma Jan 16 '12 at 13:13
  • Simply use `killBackgroundProcesses (String packageName)` and then apply trick. you need to give a if else statement. `id API is greater than 7 then use `killBackgroundProcesses (String packageName)` otherwise use `restartPackage(String packageNname)` – Sunny Jan 16 '12 at 16:45
  • BTW, i tried it in froyo and it was working fine. In Which phone you tried? – Sunny Jan 16 '12 at 16:49
  • thanks john for the reply. I tried in galaxy tab 7 and even **killBackgroundProcess()** was not working there . I thought the same trick but when i tried killBackgroundProcess() there it was not working there. Any idea...?? – Dinesh Sharma Jan 16 '12 at 18:01
  • what is happening when you call `killBackgroundProcess()` or `restartPackage()`. Did you check in log-cat ? – Sunny Jan 17 '12 at 04:23
  • **killBackgroundProcess()** is not showing anything on logcat ....bit confused .... – Dinesh Sharma Jan 17 '12 at 04:54
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/6777/discussion-between-john-smith-and-dinesh-sharma) – Sunny Jan 17 '12 at 05:57
  • hey Dinesh i found an interesting thing. go to chat room. – Sunny Jan 17 '12 at 06:56
  • @johnsmith can u pls tell me how u have solved your issue which method u use i already have all running app name and its package name how can i kill app using code ? which method is more preferrable > – Erum Nov 22 '14 at 12:00
13

In a nutshell, Automatic Task Killers work by polling the OS for a list of currently running processes and the memory they are consuming. Then either with an intelligent algorithm or with user input the Task Killers issue a call to the system telling the system to kill the process. There are two apis you can do this.

They are

  • Process.sendSignal(pid, Process.SIGNAL_KILL);
  • ActivityManager.killBackgroundProcesses(PackageName)

This first works by invoking Process.killProcess(int pid) where pid is the unique identifier for a specific process. Android kills processes in the same way that linux does; however, a user may only kill processes that they own. In Android each app is run with a unique UID, user ID. So using this API an App can only kill its own processes, hence the following explanation in the docs for Process.killProcess(int pid):

Kill the process with the given PID. Note that, though this API allows us to request to kill any process based on its PID, the kernel will still impose standard restrictions on which PIDs you are actually able to kill. Typically this means only the process running the caller's packages/application and any additional processes created by that app; packages sharing a common UID will also be able to kill each other's processes.

When this method is called the signals is generated by the OS and set to the process. Whenever a process receives a signal from the OS it must either handle that signal or immediately die. Signals such as SIG_KILL cannot be handled and result in the immediate death of the recipient process. If you want to kill processes that you don't have privileges to kill, i.e. its not your process, then you must escalate your privileges using sudo (this would require root privileges on the device).

The second API works by telling the ActivityManager that you wan to kill processes associated with a specific Package. This API gets around the need for your UID to match the UID of the process because it requires the user to accept the KILL_BACKGROUND_PROCESSES permission. This permission signals to the OS that an app has been approved by the user as a task killer. When a task killer wants to kill an app, it tells the OS to do it getting around the problem of only being able to kill processes that you own.

In the Android Docs it says that this API actually uses the first Process.killProcess API

Have the system immediately kill all background processes associated with the given package. 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.

Hope It Helps.

  • 1
    Thanks for the reply Mike. But I have tried **ActivityManager.killBackgroundProcesses(PackageName)** . And once i use this method thereafter if i call **ActivityManager.getRunningTasks(Integer.MAX_VALUE);** , it returns me the same number of task as previous , I mean the number of task never changes . Could not understand whts the reason behind it . Please help if i am doing anything wrong – Dinesh Sharma Jan 11 '12 at 13:01
  • Hi Mike , do you have any reply for my above question . Actually I am quite confused in this ....:( – Dinesh Sharma Jan 12 '12 at 10:11
6

Did you enter below permission in the manifest file?

<uses-permission android:name="android.permission.GET_TASKS"/>
<uses-permission android:name="android.permission.KILL_BACKGROUND_PROCESSES"/>
Zelldon
  • 5,396
  • 3
  • 34
  • 46
Vinayak Patil
  • 61
  • 1
  • 1