简体   繁体   中英

How to kill background processes in Android 6.0 and newest?

I use that code fragment for cleaning RAM memory:

public void onClear(){
    ActivityManager am = (ActivityManager) getApplicationContext().getSystemService(Context.ACTIVITY_SERVICE);
    for (ActivityManager.RunningAppProcessInfo pid : am.getRunningAppProcesses()) {
        am.killBackgroundProcesses(pid.processName);
        android.os.Process.killProcess(pid.uid);
    }   
}

It works correct on my phone with Android 5.1, but doesn not works on the same phone with 7.1 firmware, also does not works on other phone with Android 6.0.

I have a <uses-permission android:name="android.permission.KILL_BACKGROUND_PROCESSES" /> in the AndrodManifest.xml. In the build.gradle targetSdkVersion = 22.

Starting from Android M, system doesn't grant dangerous permissions automatically. android.permission.KILL_BACKGROUND_PROCESSES falls in dangerous permission category. You have to request permission at runtime. Tutorial to implement can be found here

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM