-1

When i press home key for long time in my android device.I get list of all recent applications. Now i want to kill all my recent applications pro grammatically from within my application. I am able to get list of all recent applications but not able to kill all apps.

I want to achieve it with android version 4.x

Can anyone guide me ?

Anirudh Ramanathan
  • 46,179
  • 22
  • 132
  • 191
Ramesh Solanki
  • 2,961
  • 4
  • 28
  • 44

2 Answers2

2

You can try Process.killProcess(). But it wont allways work:

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.

Uriel Frankel
  • 14,304
  • 8
  • 47
  • 69
0

First get a list of RunningTaskInfo records using the following:

 [public List<ActivityManager.RunningTaskInfo> getRunningTasks (int maxNum)][1]

Reference

After that, you can use the process-ids in that list to terminate the relevant processes using either

activityManager.killBackgroundProcess(pid);
                    or
android.os.Process.killProcess(pid);

depending upon whether you can kill that process. Way to kill these is detailed here.

Community
  • 1
  • 1
Anirudh Ramanathan
  • 46,179
  • 22
  • 132
  • 191
  • I tried as you guided me but still all recent apps exists.Can you please guide me more how to achieve it ? – Ramesh Solanki Jul 29 '12 at 09:16
  • See [**this**](http://stackoverflow.com/a/7560009/759019) for guidance on killing the processes, which you do not have direct privileges to kill – Anirudh Ramanathan Jul 29 '12 at 09:17
  • So what you are saying is that you need to have a rooted phone and the killing app to have escalated privileges for this to work. – wojciii Jul 29 '12 at 09:23
  • No. No. If you read through that answer, you will see `The second API works by telling the built in ActivityManager that you want to kill processes associated with a specific Package. ` because `killProcess()` can only kill processes that belong to that user. Each application runs as a different user in the Dalvik VM. Hence, the work-around with the `KILL_BACKGROUND_PROCESSES` permission. [**(Reference)**](http://developer.android.com/reference/android/app/ActivityManager.html#killBackgroundProcesses%28java.lang.String%29) – Anirudh Ramanathan Jul 29 '12 at 09:27
  • I tried by ActivityManager.killBackgroundProcesses(String packageName) but it is not working and process remains running. – Ramesh Solanki Jul 29 '12 at 09:58