17

I want to kill all running applications in android. so for this task, I've implemented the following code. But it is not working. The app still remains running.

ActivityManager manager = (ActivityManager) getSystemService(ACTIVITY_SERVICE);
    for (RunningAppProcessInfo service : manager.getRunningAppProcesses()) {
    Log.i("process name " , service.processName);
    
    android.os.Process.killProcess(service.pid);
    }

So where did I make a mistake in the code?

1
  • Let me tell you i have tested it in AVD not in device .So is there any way it only runs in real device ? Sep 13, 2011 at 6:29

4 Answers 4

24
  • You can use Process.killProcess(int pid) to kill processes that have the same UID with your App.
  • You can use ActivityManager.killBackgroundProcesses(String packageName),with KILL_BACKGROUND_PROCESSES permission in your manifest(for API >= 8) or ActivityManager.restartPackage (String packageName)(for API < 8) to kill specified process,except of forground process.

So if you would to kill all other processes when your program is foreground process,you would to use ActivityManager.killBackgroundProcesses or ActivityManager.restartPackage:

List<ApplicationInfo> packages;
    PackageManager pm;
    pm = getPackageManager();
    //get a list of installed apps.
    packages = pm.getInstalledApplications(0);

   ActivityManager mActivityManager = (ActivityManager)context.getSystemService(Context.ACTIVITY_SERVICE);
   String myPackage = getApplicationContext().getPackageName();
   for (ApplicationInfo packageInfo : packages) {
        if((packageInfo.flags & ApplicationInfo.FLAG_SYSTEM)==1)continue;
        if(packageInfo.packageName.equals(myPackage)) continue;
        mActivityManager.killBackgroundProcesses(packageInfo.packageName);
   }      

In above snippet code,each process will be killed unless it be process of your App or system process.
References:
How to close all active applications from my Android app?
How do task killers work?

6
  • I dont understand what the line -------- if((packageInfo.flags & ApplicationInfo.FLAG_SYSTEM)==1)continue; does..!! could you please explain
    – Lalith B
    Nov 29, 2012 at 12:23
  • 1
    @lalith If ((packageInfo.flags & ApplicationInfo.FLAG_SYSTEM)==1) be true,it means that this App is a system App (and we have not stop a system App),so we continue. Nov 29, 2012 at 12:40
  • 3
    will it remove apps from recent apps list (displayed by long pressing the home button)? Jan 26, 2014 at 18:37
  • @UmerFarooq I mentioned it before in the answer: In above snippet code,each process will be killed unless it be process of your App or system process. Jan 27, 2014 at 2:32
  • 1
    recent apps are not being removed
    – Kaushal28
    Nov 21, 2017 at 17:33
5

You have another possibility if the device is rooted (has superuser rights).

You can invoke an external process that would use the su rights. But note that it's probably a bad design as Android OS should be the only one to kill processes.

try {
   Process rootProcess = Runtime.getRuntime().exec(new String[] { "su" });

   String command = "kill - 9 <pid>";

   BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(rootProcess.getOutputStream()), 2048);
   try {
      bw.write(command);
      bw.newLine();
      bw.flush();
   } catch (IOException e) {
    // Handle error
   }
} catch (Exception e) {
   e.printStackTrace();
   // Device not rooted!            
}
2
  • I tested it and still app running but killed Sep 13, 2011 at 9:05
  • Your phone has to be rooted
    – Overblade
    Dec 9, 2016 at 16:22
2

According to doc enter link description here

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

I test it, and it only kill process hosted my App.

2
  • 1
    Then is it impossible to kill others running applications ? Sep 13, 2011 at 7:17
  • 2
    I do little search about killing external app. But, nothing usefull find. In the other hand, we have many Task Managers app who can kill any external process. So, this is possible - but unfortunatelly i don't know how yet.
    – Bandzio
    Sep 13, 2011 at 8:01
1

Reflect to force stop

ActivityManager am = (ActivityManager) getSystemService(Context.ACTIVITY_SERVICE); Method method = Class.forName("android.app.ActivityManager").getMethod("forceStopPackage", String.class); method.invoke(am, pkgName);

You should add this system permission:

android.permission.FORCE_STOP_PACKAGES

1
  • 1
    android studio warning that it will only be granted to system apps. Jan 12, 2018 at 1:30

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.