Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to kill all running applications in android?

Tags:

android

I want to kill all running applications in android .so for this task , i implemented following code. But it is not working .App still remains in 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 does i make mistake in code ? Is there any help ?

like image 528
Nandlal Virani Avatar asked Sep 13 '11 06:09

Nandlal Virani


People also ask

How do you kill an Android?

Walk backwards and keep the android at a safe distance with the Bolt Gun aimed right at their head. This is a one shot kill weapon against any android and is the most effective way to eliminate a suited android.


2 Answers

  • 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?

like image 111
hasanghaforian Avatar answered Sep 18 '22 03:09

hasanghaforian


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!            
}
like image 21
aymeric Avatar answered Sep 22 '22 03:09

aymeric