Clear RAM

public class OsClearRam {
    private static final String TAG = "OsClearRam";
    public static final int REFRESH_RAM_UI_START = 1;
    public static final int REFRESH_RAM_UI_END = 2;

    private Context mContext;


    private boolean mIsKilling = false;
    private ArrayList<String> mNoKilllist = new ArrayList<String>();

    private List<ActivityManager.RecentTaskInfo> mCurrentRrecentTasks = null;
    private List<ActivityManager.RecentTaskInfo> mNeedClearRecentTasks =
            new ArrayList<ActivityManager.RecentTaskInfo>();

    private PackageManager mPackageManager;
    private ActivityManager mActivityManager;

    private Handler mHandler;
    public OsClearRam(Context context,Handler handler){
        mContext = context;
        mHandler = handler;
        mActivityManager = (ActivityManager)mContext.getApplicationContext().getSystemService(Context.ACTIVITY_SERVICE);
        mPackageManager = mContext.getPackageManager();
    }


    /**
     * Clear Ram
     */
    public void recentRemoveAll(){
        if(mIsKilling){
            return;
        }

        //clear start
        mHandler.sendEmptyMessage(REFRESH_RAM_UI_START);

        mNoKilllist.clear();

        try {
            int maxTasks = 21;
            mCurrentRrecentTasks = mActivityManager.getRecentTasks(maxTasks, ActivityManager.RECENT_IGNORE_UNAVAILABLE);
            int numTasks = mCurrentRrecentTasks.size();

            ActivityInfo homeInfo = new Intent(Intent.ACTION_MAIN).addCategory(Intent.CATEGORY_HOME).resolveActivityInfo(mPackageManager, 0);

            mNeedClearRecentTasks = new ArrayList<ActivityManager.RecentTaskInfo>();

            for (int i = 0, index = 0; i < numTasks && (index < maxTasks); ++i) {
                ActivityManager.RecentTaskInfo recentInfo = mCurrentRrecentTasks.get(i);
                Intent intent = new Intent(recentInfo.baseIntent);

                if (recentInfo.origActivity != null) {
                    intent.setComponent(recentInfo.origActivity);
                }
                //Don't kill this
                mNoKilllist.add(intent.getComponent().getPackageName());

                //Log.d(TAG, "recentRemoveAll():intent.getComponent()="+intent.getComponent());

                mNeedClearRecentTasks.add(recentInfo);
            }


            for(int i = 0; i < mNeedClearRecentTasks.size(); i++){
                final ActivityManager.RecentTaskInfo recentInfo = mNeedClearRecentTasks.get(i);
                Intent intent = new Intent(recentInfo.baseIntent);
                mActivityManager.removeTask(mNeedClearRecentTasks.get(i).persistentId);
            }
        } catch (SecurityException e) {
            Log.e(TAG, "RecentsActivity.recentRemoveAll()--Exception!!");
        }

        //kill process
        dokillProcess();
    }
    private void dokillProcess(){
        new Thread(new Runnable() {
            @Override
            public void run() {
                     mIsKilling =true;
                     if (mContext == null){
                         return;
                     }
                     List<RunningAppProcessInfo>  mRunningPros = mActivityManager.getRunningAppProcesses();
                     for (ActivityManager.RunningAppProcessInfo amPro : mRunningPros){

                         int[] myMempid = new int[] {amPro.pid};
                         Debug.MemoryInfo[] memoryInfo = mActivityManager.getProcessMemoryInfo(myMempid);
                         double memSize = memoryInfo[0].dalvikPrivateDirty/1024.0;
                         int temp = (int)(memSize*100);
                         memSize = temp/100.0;
                         ApplicationInfo info = null;
                         try {
                             info = mPackageManager.getApplicationInfo(amPro.processName, 0);
                         } catch (NameNotFoundException e) {
                             Log.d(TAG, "NameNotFoundException");
                         }
                         String ProInfo="";
                         ProInfo +="Name:"+ amPro.processName
                                 + "\nID:" + amPro.pid
                                 + "\nMemory:" + memSize + "MB"+"\n apinfo.importance:"+amPro.importance;
                         //Log.v(TAG,"---------proinfo="+ProInfo);
                         if(amPro.importance>ActivityManager.RunningAppProcessInfo.IMPORTANCE_SERVICE){
                             for(int j=0;j<pkgList.length;j++){
                                 if(!mNoKilllist.contains(pkgList[j])){
                                     mActivityManager.killBackgroundProcesses(pkgList[j]);
                                 }
                             }
                         }

                     }

                     //Clear Ram end
                     mHandler.sendEmptyMessage(REFRESH_RAM_UI_END);

                     mIsKilling =false;
            }
        }).start();
    }


    private boolean isCurrentHomeActivity(ComponentName component, ActivityInfo homeInfo) {
        if (homeInfo == null) {
            final PackageManager pm = mContext.getPackageManager();
            homeInfo = new Intent(Intent.ACTION_MAIN).addCategory(Intent.CATEGORY_HOME)
                .resolveActivityInfo(pm, 0);
        }
        return homeInfo != null
               && homeInfo.packageName.equals(component.getPackageName())
               && homeInfo.name.equals(component.getClassName());
    }

   /**
    * Added to check whether fm is active.
    *
    * @return true if any fm are active.
    */
    private boolean isFMActive(){
        AudioManager audioManager = (AudioManager)mContext.getSystemService(Context.AUDIO_SERVICE);
        String FmStatus = AudioSystem.getParameters("GetFmEnable");

        //Log.d(TAG, "isFmActive  " + FmStatus);

        if (FmStatus.compareTo("GetFmEnable=true") == 0) {
             return true;
        } else {
             return false;
        }
    }


}

猜你喜欢

转载自blog.csdn.net/liu362732346/article/details/80321457
RAM
今日推荐