23

I want to close all recent apps at once. I found the following code, but it only shows recent apps. I want to close all these recent apps.

Show all recent app list code: (but I don't want list i need to close all recent apps)

Class serviceManagerClass = Class.forName("android.os.ServiceManager");
Method getService = serviceManagerClass.getMethod("getService", String.class);
IBinder retbinder = (IBinder) getService.invoke(serviceManagerClass, "statusbar");
Class statusBarClass = Class.forName(retbinder.getInterfaceDescriptor());
Object statusBarObject = statusBarClass.getClasses()[0].getMethod("asInterface", IBinder.class).invoke(null, new Object[] { retbinder });
Method clearAll = statusBarClass.getMethod("toggleRecentApps");
clearAll.setAccessible(true);
clearAll.invoke(statusBarObject);
Toast.makeText(getApplicationContext(), "All recent app closed....", Toast.LENGTH_LONG).show();
Harshad Pansuriya
  • 20,189
  • 8
  • 67
  • 95
sms247
  • 4,404
  • 5
  • 35
  • 45
  • 1
    http://stackoverflow.com/questions/28799357/how-to-close-kill-an-android-app-programmatically-without-leaving-it-in-the-back – Randyka Yudhistira Jan 15 '16 at 10:28
  • it just hide your current app from recent app list ,my problem is to clear or close all opened recent apps – sms247 Jan 15 '16 at 10:31

3 Answers3

10

No, it's not possible to kill & remove from "Recent apps" list user's recent apps, even with Process.killProcess() method. Read these two answers:
- Kill another application on Android?
- How to kill currently running task in android

However, what you can do is to restart the background processes of the recent apps (that's what most TaskKillers from Google Play actually do):

  • Add KILL_BACKGROUND_PROCESSES user-permission into your manifest:

    <uses-permission android:name="android.permission.KILL_BACKGROUND_PROCESSES"/>
    
  • And then killBackgroundProcesses(previously known as restartPackage) for the process/processes you want to "kill" (even though it's not actual killing)

    ActivityManager actvityManager = (ActivityManager)
            this.getSystemService( ACTIVITY_SERVICE );
    List<ActivityManager.RunningAppProcessInfo> procInfos = actvityManager.getRunningAppProcesses();
    for(ActivityManager.RunningAppProcessInfo runningProInfo:procInfos){
        if (runningProInfo.processName.equals("PACKAGE_NAME_YOU_WANT_TO_KILL")) {
            actvityManager.killBackgroundProcesses(runningProInfo.processName);
        }
    }
    

As a result, "killed" apps, no longer presented in the list of running process, they are removed from memory. Apps are still in list of recent apps and you can't do anything with it - see answer clear all recent task Programmaticcally

I hope, it helps.

Community
  • 1
  • 1
Konstantin Loginov
  • 15,802
  • 5
  • 58
  • 95
  • what is "PACKAGE_NAME_YOU_WANT_TO_KILL" i think it should procInfos.Name what you think?and also i think it will only kill running apps i want to clear all apps which shows in recent history list which show on "Home" button long press – sms247 Jan 26 '16 at 05:31
  • @sms247 for example, `com.android.chrome`(i.e processName actually equals to the app's package), once you want to stop Chrome browser. To get list of only recently running apps - use `getRunningTasks` (http://stackoverflow.com/questions/6870487/how-to-make-a-list-of-recent-running-apps). But again, they **won't** be removed from the list of recent apps (unless you are device manufacturer) – Konstantin Loginov Jan 26 '16 at 08:05
6

Quitting an application is the job of the Android OS. Consider the reasons you want to quit all the recent applications. If all the recent applications are not written properly, with respect to the Android OS application development model, and they're misbehaving, then they should be uninstalled or fixed.

Here is a wonderful wiki on the subject:

Is quitting an application frowned upon?

Community
  • 1
  • 1
activedecay
  • 10,129
  • 5
  • 47
  • 71
  • i know its not possible but i have idea if i got code which will return all process ids then i use this line Process.killProcess(Process.myPid()) in forloop so kill all apps but i am not sure it will also clear from recent app list – sms247 Jan 21 '16 at 06:55
  • i'm not sure if my answer is clear to you, so let me try again here: don't kill apps like that. – activedecay Jan 21 '16 at 07:01
0


First is to get all activities running as what Konstantin Loginov said but instead of having it killed like putting its name. It may be better if checking also includes verification of process names listed under your recent apps if they are in your running processes.

After that, remove the list under your recent apps. You may want to take a look at this thread.

Community
  • 1
  • 1
bipartite
  • 91
  • 7