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 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

xiaowang_lj

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值