清理内存clear

清理内存clear:

package com.android.cleanprocesstool;

import android.content.BroadcastReceiver;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.content.pm.ResolveInfo;
import android.util.Log;
import android.os.UserHandle;
import android.os.UserManager;

import java.util.List;
import android.app.Activity;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.BatteryManager;
import android.os.BatteryStats;
import android.os.Bundle;
import android.os.Handler;
import android.os.IPowerManager;
import android.os.Message;
import android.os.ServiceManager;
import android.os.SystemClock;
import android.text.format.DateUtils;
import android.widget.TextView;
import android.os.Bundle;
import android.app.ActivityManager;
import android.text.format.Formatter;
import android.util.Log;
import android.widget.Toast;
import java.util.List;

public class ClearMemReceiver extends BroadcastReceiver {
    private static final String TAG = "Settings";
    private static final String PRIMARY_PROFILE_SETTING =
        "com.android.settings.PRIMARY_PROFILE_CONTROLLED";

    ActivityManager.MemoryInfo memoryInfo;
    private ActivityManager myActivityManager;
    private Context context;
    long mBeforeClearMem = 0;
    long mAfterClearMem = 0;

    private final long MIN_MEM_CLEAR = 10 * 1024 * 1024;
    @Override
    public void onReceive(Context context, Intent broadcast) {
        context = context;
        memoryInfo = new ActivityManager.MemoryInfo();
        myActivityManager =(ActivityManager)context.getSystemService(Activity.ACTIVITY_SERVICE);
        myActivityManager.getMemoryInfo(memoryInfo) ;

        mBeforeClearMem = memoryInfo.availMem;
        clearBackgroundProcesses(context);
    }

    private String[] mWhiteList = {
        "com.tencent.qqmusic",
        "com.tencent.mobileqq"
    };

    private void clearBackgroundProcesses(Context context) {

        ActivityManager activityManger = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
        List<ActivityManager.RunningAppProcessInfo> list = activityManger.getRunningAppProcesses();

        int importance = ActivityManager.RunningAppProcessInfo.IMPORTANCE_SERVICE;
        if (list != null) {
            for(int i = 0; i < list.size(); i++) {
                ActivityManager.RunningAppProcessInfo apinfo = list.get(i);
                String[] pkgList = apinfo.pkgList;
                for (int j = 0; j < pkgList.length; j++) {
                    boolean inWhiteList = false;
                    for  (String whitelist : mWhiteList) {
                        if (whitelist.equals(pkgList[j])) {
                            inWhiteList = true;
                            break;
                        }
                    }
                    if (!inWhiteList && !pkgList[j].startsWith("com.android.providers")) {
                        activityManger.forceStopPackage(pkgList[j]);
                        
                    }
                }
            }
        }
        myActivityManager =(ActivityManager)context.getSystemService(Activity.ACTIVITY_SERVICE);
        myActivityManager.getMemoryInfo(memoryInfo) ;
        mAfterClearMem = memoryInfo.availMem;
        if (mAfterClearMem - mBeforeClearMem < MIN_MEM_CLEAR) {
            Toast.makeText(context, context.getResources().getString(R.string.clear_mem_no_need), Toast.LENGTH_LONG).show();

        } else {
            String memInfoString = Formatter.formatFileSize(context, mAfterClearMem - mBeforeClearMem);
            String toast = context.getResources().getString(R.string.clear_mem_release);
            Toast.makeText(context, String.format(toast, memInfoString), Toast.LENGTH_SHORT).show();

        }
    }

}

 

posted @ 2015-09-18 11:32  williamgufeng  阅读(440)  评论(0编辑  收藏  举报