Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

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(); 
like image 702
sms247 Avatar asked Jan 15 '16 10:01

sms247


People also ask

How do I close all apps automatically on Android?

To close apps on Android, swipe up from the bottom of the screen and hold until the recent apps menu pops up (if you use gesture navigations). If you use button navigation, tap on the recent apps button. Swipe up to close individual apps or tap the Close all button to close all background apps.

How do I set my phone to automatically close apps?

Once the Developer options interface comes up, from the top-right corner, slide to set Developer options toggle button to ON. On the same interface, under the APPS section, tap to check the Don't keep activities checkbox in order to close the running activities automatically when they are not in use.


1 Answers

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.

like image 95
Konstantin Loginov Avatar answered Nov 14 '22 14:11

Konstantin Loginov