sms247
sms247

Reputation: 4504

Close all recent open/running apps at once button-android

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

Upvotes: 23

Views: 6402

Answers (3)

bipartite
bipartite

Reputation: 101


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.

Upvotes: 0

Konstantin Loginov
Konstantin Loginov

Reputation: 16010

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.

Upvotes: 10

activedecay
activedecay

Reputation: 10867

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?

Upvotes: 6

Related Questions