0

i'm trying to find way how to kill currently task in android Marshmallow i already have two methods but the both works fine in android 4.0.3 and android 5.0.1 but when i trying in android 6.0.1 she doesn't work any idea for resolve this problem !,

List<ApplicationInfo> packages;
            PackageManager pm;
            pm = getPackageManager();
            //get a list of installed apps.
            packages = pm.getInstalledApplications(0);
            ActivityManager mActivityManager = (ActivityManager)getApplicationContext().getSystemService(Context.ACTIVITY_SERVICE);
            String myPackage = getApplicationContext().getPackageName();
            for (ApplicationInfo packageInfo : packages) {
                if((packageInfo.flags & ApplicationInfo.FLAG_SYSTEM)==1)continue;
                if(packageInfo.packageName.equals(myPackage)) continue;
                mActivityManager.killBackgroundProcesses(packageInfo.packageName);
            }

and this method :

ActivityManager am = (ActivityManager) getApplicationContext().getSystemService(Context.ACTIVITY_SERVICE);
            for (ActivityManager.RunningAppProcessInfo pid : am.getRunningAppProcesses()) {
                am.killBackgroundProcesses(pid.processName);
            }

1 Answers1

0

Try using this in your second method.

android.os.Process.killProcess(pid);

Kill the process with the given PID. Note that, though this API allows us to request to kill any process based on its PID, the kernel will still impose standard restrictions on which PIDs you are actually able to kill. Typically this means only the process running the caller's packages/application and any additional processes created by that app; packages sharing a common UID will also be able to kill each other's processes. Taken from here

I also recommend getting your application id by using

android.os.Process.myPid();
Ashwin Prasad
  • 79
  • 1
  • 12