Cata
Cata

Reputation: 11211

android task kill

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!

Upvotes: 8

Views: 19843

Answers (5)

bastami82
bastami82

Reputation: 6173

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)

Upvotes: 0

europa
europa

Reputation: 23

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.

Upvotes: 1

Midhun VP
Midhun VP

Reputation: 629

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);

Upvotes: 2

CommonsWare
CommonsWare

Reputation: 1007474

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

Upvotes: 10

CoolStraw
CoolStraw

Reputation: 5390

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

Upvotes: 4

Related Questions