android task kill

19,500

Solution 1

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

Solution 2

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

Solution 3

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

Solution 4

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.

Share:
19,500
Cata
Author by

Cata

Eager to learn and do new things. I consider myself an open minded person and I appreciate any feedback received because admitting your mistakes it's a good start to make improvements! ;)

Updated on June 04, 2022

Comments

  • Cata
    Cata almost 2 years

    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
    Cata about 13 years
    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
    Cata about 13 years
    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 ...
  • CommonsWare
    CommonsWare about 13 years
    @Cata: "yes but it didn't supose to do a force close?" -- no. Please read the documentation for killProcess().
  • Cata
    Cata about 13 years
    I have read it, and it says "Kill the process with the given PID." and it doesn't say that I need some rights...
  • CommonsWare
    CommonsWare about 13 years
    @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."
  • Cata
    Cata about 13 years
    Sorry CommonsWare, you are right I didn't read the full doc... thank you for the help!
  • Ankitkumar Makwana
    Ankitkumar Makwana over 11 years
    @CommonsWare same question i have i want to kill process but could not kill so, its not possible any helps appreciate me .
  • Behnam
    Behnam over 10 years
    why on back pressesd, is it some kind of hack?!