0

I want to build upon my battery saving android app and add the functionality to close all apps excluding user-specified ones.

Im not asking how to kill a process, I want to close the recent apps in the same way that you would by hitting your recent apps button and swiping them all away.

Does anyone know of a way to do this?

JayB
  • 397
  • 6
  • 21
  • 1
    You can only kill your own apps. You can not kill other apps unless the device is rooted. Take a look at [this](http://stackoverflow.com/questions/8814696/how-to-kill-currently-running-task-in-android/8849562#8849562). – Hemanth Feb 25 '15 at 09:12
  • @JayB:Let me know the below answer is helps you or not? – Born To Win Mar 18 '15 at 09:56

1 Answers1

0

It is not recommanded but it is posible by using killBackgroundProcesses of Activitymanager.

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

    ActivityManager mActivityManager = (ActivityManager)context.getSystemService(Context.ACTIVITY_SERVICE);

   for (ApplicationInfo packageInfo : packages) {
        if((packageInfo.flags & ApplicationInfo.FLAG_SYSTEM)==1)continue;
        if(packageInfo.packageName.equals("mypackage")) continue;
        mActivityManager.killBackgroundProcesses(packageInfo.packageName);
   }

Hope it will helps you.

Born To Win
  • 3,319
  • 3
  • 19
  • 27