0

I have one requirement like, I want to close all background running applications. I used this code, but its not working...

    ActivityManager  manager =  (ActivityManager)this.getSystemService(Context.ACTIVITY_SERVICE);
    List<ActivityManager.RunningAppProcessInfo> listOfProcesses = manager.getRunningAppProcesses();
    for (ActivityManager.RunningAppProcessInfo process : listOfProcesses)
    {
        System.out.println(process.processName);
        manager.killBackgroundProcesses(process.processName);
    }

Any help?

Anil kumar
  • 1,534
  • 3
  • 14
  • 20

2 Answers2

0

You can only kill your own apps, e.g. the ones running in the same process or with the same userID. You can not kill others, unless the device is rooted.

have a look at this answer. How to kill currently running task in android

Your problem will be solved now

Community
  • 1
  • 1
Hassaan Rabbani
  • 2,469
  • 5
  • 30
  • 55
0

Try this

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

    for (ApplicationInfo packageInfo : packages) {
        if ((packageInfo.flags & ApplicationInfo.FLAG_SYSTEM) == 1)
            continue;
        if (packageInfo.packageName.equals("mypackage"))
            continue;
        mActivityManager.killBackgroundProcesses(packageInfo.packageName);
    }
Suresh Sharma
  • 1,826
  • 22
  • 41