Android 设备清理内存 RAM

实现思路 调用系统的killBackgroundProcesses

具体实现如下:

1.配置权限

<uses-permission android:name="android.permission.KILL_BACKGROUND_PROCESSES"/>

2.具体实现流程

public class ClearMemoryActivity extends Activity {  
    private static final String TAG = "ClearMemoryActivity";  
  
    /** 
     * Called when the activity is first created. 
     */  
    @Override  
    public void onCreate(Bundle savedInstanceState) {  
        super.onCreate(savedInstanceState);  
        setContentView(R.layout.main);  
  
        Button clear = (Button) findViewById(R.id.clear);  
        clear.setOnClickListener(new View.OnClickListener() {  
            @Override  
            public void onClick(View v) {  
                //To change body of implemented methods use File | Settings | File Templates.  
                ActivityManager am = (ActivityManager) getSystemService(Context.ACTIVITY_SERVICE);  
                List<RunningAppProcessInfo> infoList = am.getRunningAppProcesses();  
                List<ActivityManager.RunningServiceInfo> serviceInfos = am.getRunningServices(100);  
  
                long beforeMem = getAvailMemory(ClearMemoryActivity.this);  
                Log.d(TAG, "-----------before memory info : " + beforeMem);  
                int count = 0;  
                if (infoList != null) {  
                    for (int i = 0; i < infoList.size(); ++i) {  
                        RunningAppProcessInfo appProcessInfo = infoList.get(i);  
                        Log.d(TAG, "process name : " + appProcessInfo.processName);  
                        //importance 该进程的重要程度  分为几个级别,数值越低就越重要。  
                        Log.d(TAG, "importance : " + appProcessInfo.importance);  
  
                        // 一般数值大于RunningAppProcessInfo.IMPORTANCE_SERVICE的进程都长时间没用或者空进程了  
                        // 一般数值大于RunningAppProcessInfo.IMPORTANCE_VISIBLE的进程都是非可见进程,也就是在后台运行着  
                        if (appProcessInfo.importance > RunningAppProcessInfo.IMPORTANCE_VISIBLE) {  
                            String[] pkgList = appProcessInfo.pkgList;  
                            for (int j = 0; j < pkgList.length; ++j) {//pkgList 得到该进程下运行的包名  
                                Log.d(TAG, "It will be killed, package name : " + pkgList[j]);  
                                am.killBackgroundProcesses(pkgList[j]);  
                                count++;  
                            }  
                        }  
  
                    }  
                }  
  
                long afterMem = getAvailMemory(ClearMemoryActivity.this);  
                Log.d(TAG, "----------- after memory info : " + afterMem);  
                Toast.makeText(ClearMemoryActivity.this, "clear " + count + " process, "  
                            + (afterMem - beforeMem) + "M", Toast.LENGTH_LONG).show();  
            }  
        });  

    }  
  
    //获取可用内存大小  
    private long getAvailMemory(Context context) {  
        // 获取android当前可用内存大小  
        ActivityManager am = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);  
        MemoryInfo mi = new MemoryInfo();  
        am.getMemoryInfo(mi);  
        //mi.availMem; 当前系统的可用内存  
        //return Formatter.formatFileSize(context, mi.availMem);// 将获取的内存大小规格化  
        Log.d(TAG, "可用内存---->>>" + mi.availMem / (1024 * 1024));  
        return mi.availMem / (1024 * 1024);  
    }  
}  

注意:getRunningAppProcesses()只能获取到自身app应用的信息,如果要获取其他应用的信息,需要将该apk设置为 "android.uid.system" 或系统签名

(220条消息) Android P 性能优化:创建APP进程白名单,杀死白名单之外的进程_killbackgroundprocesses的黑白名单配置_阿迷创客的博客-CSDN博客

(221条消息) Android中杀进程的几种方法 (1) - killBackgroundProcesses_Jtag特工的博客-CSDN博客_kill_background_processes 

猜你喜欢

转载自blog.csdn.net/xiaowang_lj/article/details/128964981
RAM