简体   繁体   English

如何终止Android 6.0及最新版本的后台进程?

[英]How to kill background processes in Android 6.0 and newest?

I use that code fragment for cleaning RAM memory: 我使用该代码片段清洁RAM内存:

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. 它可以在装有Android 5.1的手机上正常工作,但不能在装有7.1固件的手机上正常工作,也不能在装有Android 6.0的其他手机上工作。

I have a <uses-permission android:name="android.permission.KILL_BACKGROUND_PROCESSES" /> in the AndrodManifest.xml. 我在AndrodManifest.xml中有一个<uses-permission android:name="android.permission.KILL_BACKGROUND_PROCESSES" /> In the build.gradle targetSdkVersion = 22. 在build.gradle中targetSdkVersion = 22。

Starting from Android M, system doesn't grant dangerous permissions automatically. 从Android M开始,系统不会自动授予危险权限。 android.permission.KILL_BACKGROUND_PROCESSES falls in dangerous permission category. android.permission.KILL_BACKGROUND_PROCESSES属于危险许可类别。 You have to request permission at runtime. 您必须在运行时请求权限。 Tutorial to implement can be found here 可以在此处找到实施教程

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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