8

I want to kill all tasks that run in android like task killer... What I have done until now is:

ActivityManager manager =  (ActivityManager) this.getSystemService(ACTIVITY_SERVICE);
    List<RunningAppProcessInfo> activityes = ((ActivityManager) manager).getRunningAppProcesses();

    for (int i = 0; i < activityes.size(); i++){

        Log.e("APP: "+i, activityes.get(0).processName);

        if (!activityes.get(0).processName.equals("app.android.myapp")){
            Process.killProcess(activityes.get(0).pid);
        }

    }

The problem with the code is that it returns in the activityes list only my app for 12 times. And no task is being killed...

Can somebody help me please? Thank you!

Cata
  • 11,133
  • 11
  • 65
  • 86

5 Answers5

10

You do not have the rights to kill other processes; hence, killProcess() does not work for your app.

CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491
  • yes but it didn't supose to do a force close? I have android:name="android.permission.GET_TASKS" permission, and in the log I can see: com.svox.pico Sending signal. PID: 328 SIG: 9 jp.co.omronsoft.openwnn Sending signal. PID: 141 IG: 9 system Sending signal. PID: 71 SIG: 9 com.android.defcontainer Sending signal. PID: 176 SIG: 9 com.android.launcher Sending signal. PID: 190 SIG: 9 android.process.media Sending signal. PID: 260 SIG: 9 com.android.quicksearchbox : Sending signal. PID: 234 SIG: 9 com.android.protips Sending signal. PID: 243 SIG: 9 ... – Cata Feb 07 '11 at 13:24
  • @Cata: "yes but it didn't supose to do a force close?" -- no. Please read the documentation for `killProcess()`. – CommonsWare Feb 07 '11 at 13:40
  • I have read it, and it says "Kill the process with the given PID." and it doesn't say that I need some rights... – Cata Feb 08 '11 at 06:20
  • 6
    @Cata: You need to learn how to read. The full Javadoc is: "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." – CommonsWare Feb 08 '11 at 12:54
  • 1
    Sorry CommonsWare, you are right I didn't read the full doc... thank you for the help! – Cata Feb 09 '11 at 09:51
  • @CommonsWare same question i have i want to kill process but could not kill so, its not possible any helps appreciate me . – Ankitkumar Makwana Sep 22 '12 at 12:32
4

You're using 0 (zero) instead of i inside your loop.

for (int i = 0; i < activityes.size(); i++){

    Log.e("APP: "+i, activityes.get(i).processName);

    if (!activityes.get(i).processName.equals("app.android.myapp")){
        Process.killProcess(activityes.get(i).pid);
    }

}

Cheers

CoolStraw
  • 5,282
  • 8
  • 42
  • 64
  • Yes, now I had noticed =)) I am so dizzy today... the problem is that this don't close the apps. Now I can see them but this code dont close them. – Cata Feb 07 '11 at 12:40
2

You can kill the current process on back pressed using the following code:

public void onBackPressed() {
    super.onBackPressed();
    int pid = android.os.Process.myPid();
    android.os.Process.killProcess(pid);
Shawn Chin
  • 84,080
  • 19
  • 162
  • 191
Midhun VP
  • 629
  • 1
  • 10
  • 18
1

You can try this to kill your task or app:

ActivityManager am = (ActivityManager) ctx
                .getSystemService(ctx.ACTIVITY_SERVICE);
am.killBackgroundProcesses(packageName);

this works for 2.2 and up.

europa
  • 23
  • 7
0

1- Add to manifest

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

2 - In your code

Runtime.getRuntime().exec("adb shell killall com.example.app");

note that your app need to have access to adb shell (system app)

bastami82
  • 5,955
  • 7
  • 33
  • 44