82.android 简单的当前运行内存清理

//在Activity里使用:

//我的这个是指定了一个包名,除了这个包名的应用,其他全部清理 

private void CleaningOperation() {
        ActivityManager am = (ActivityManager) getSystemService(Context.ACTIVITY_SERVICE);
        List<ActivityManager.RunningAppProcessInfo> infoList = am.getRunningAppProcesses();
        List<ActivityManager.RunningServiceInfo> serviceInfos = am.getRunningServices(100);

//                Log.d("TAGDD1", "-----------before memory info : " +this.getAvailMemory(getApplicationContext()));
        int count = 0;
        if (infoList != null) {
            for (int i = 0; i < infoList.size(); ++i) {
                ActivityManager.RunningAppProcessInfo appProcessInfo = infoList.get(i);
                Log.e("TAGDD1", "process name : ----------" + appProcessInfo.processName);
                //importance 该进程的重要程度  分为几个级别,数值越低就越重要。
                Log.e("TAGDD2", "importance : -----------" + appProcessInfo.importance);

                // 一般数值大于RunningAppProcessInfo.IMPORTANCE_SERVICE的进程都长时间没用或者空进程了
                // 一般数值大于RunningAppProcessInfo.IMPORTANCE_VISIBLE的进程都是非可见进程,也就是在后台运行着
//                        if (appProcessInfo.importance > 100) {
//                            String[] pkgList = appProcessInfo.pkgList;
//                            for (int j = 0; j < pkgList.length; ++j) {//pkgList 得到该进程下运行的包名
//                                Log.e("TAGDD4", "It will be killed, package name : " + pkgList[j]);
//                                am.killBackgroundProcesses(pkgList[j]);
//                                count++;
//                            }
//                        }

                //只要不是com.example.hasee.a1011ceshi这个包名的进程,其余进程全部禁止
                if(!appProcessInfo.processName.equals("com.example.hasee.a1011ceshi")) {
                    String[] pkgList = appProcessInfo.pkgList;
                    for (int j = 0; j < pkgList.length; ++j) {//pkgList 得到该进程下运行的包名
                        Log.e("TAGDD3", "It will be killed, package name : ----------" + pkgList[j]);
                        am.killBackgroundProcesses(pkgList[j]);
                        count++;
                        Log.e("TAGDD4", "count : ----------" + count+"");
                    }
                }

            }
        }
    }

//权限:

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

2:

 private void clearMemory() {
        ActivityManager am = (ActivityManager) getContext().getSystemService(Context.ACTIVITY_SERVICE);
        List<ActivityManager.RunningAppProcessInfo> infoList = am.getRunningAppProcesses();

        int count = 0;
        if (infoList != null) {
            for (int i = 0; i < infoList.size(); ++i) {
                ActivityManager.RunningAppProcessInfo appProcessInfo = infoList.get(i);
//                Log.i(getClass().getSimpleName(), "process name : ----------" + appProcessInfo.processName);
                //importance 该进程的重要程度  分为几个级别,数值越低就越重要。
//                Log.i(getClass().getSimpleName(), "importance : -----------" + appProcessInfo.importance);

                //只要不是com.tq.recorder这个包名的全部进程给禁止了
                if (!appProcessInfo.processName.equals("com.tq.recorder")) {
                    String[] pkgList = appProcessInfo.pkgList;
                    for (int j = 0; j < pkgList.length; ++j) {//pkgList 得到该进程下运行的包名
                        Log.e(getClass().getSimpleName(), "It will be killed, package name : ----------" + pkgList[j]);
                        am.killBackgroundProcesses(pkgList[j]);
                        count++;
                        Log.e(getClass().getSimpleName(), "count : ----------" + count + "");
                    }
                }
            }
        }

        //获取运行内存大小
        ActivityManager.MemoryInfo mi = new ActivityManager.MemoryInfo();
        am.getMemoryInfo(mi);
        long availMem = mi.availMem;
        long ava = availMem / 1024 / 1024;
        //开始清理后台运行内存,并显示到文本上
        mCleanMemory.setText("可用内存 " + (ava >= 1024 ? (String.format("%.2f", (float) ava / 1024) + "G") : (ava + "M")));//mCleanMemory TextView框显示当前运行内存
        Toast.makeText(getContext(), "清理完毕,当前可用运行内存:" + mCleanMemory.getText().subSequence(4, mCleanMemory.getText().length()), Toast.LENGTH_SHORT).show();
    }

 //------------------------------------------------------------------完------------------------------------------------------------------------------

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值