国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 系統 > Android > 正文

Android 一鍵清理、內存清理功能實現

2019-12-12 03:56:20
字體:
來源:轉載
供稿:網友

360桌面、金山清理大師等都提供了一鍵清理、一鍵加速等功能,其實就是殺一些后臺進程來達到釋放內存的目的。

基本思路就是列出所有運行的進程,查看其重要值(RunningAppProcessInfo.importance,值越大說明進程重要程度越低),可以設定一個閾值,如果該進程的重要值大于該閾值,就可以殺掉該進程。

進程的重要值有以下幾個等級:

/**         * Constant for {@link #importance}: this is a persistent process.         * Only used when reporting to process observers.         * @hide         */        public static final int IMPORTANCE_PERSISTENT = 50;            /**         * Constant for {@link #importance}: this process is running the         * foreground UI.         */        public static final int IMPORTANCE_FOREGROUND = 100;                /**         * Constant for {@link #importance}: this process is running something         * that is actively visible to the user, though not in the immediate         * foreground.         */        public static final int IMPORTANCE_VISIBLE = 200;                /**         * Constant for {@link #importance}: this process is running something         * that is considered to be actively perceptible to the user. An         * example would be an application performing background music playback.         */        public static final int IMPORTANCE_PERCEPTIBLE = 130;                /**         * Constant for {@link #importance}: this process is running an         * application that can not save its state, and thus can't be killed         * while in the background.         * @hide         */        public static final int IMPORTANCE_CANT_SAVE_STATE = 170;                /**         * Constant for {@link #importance}: this process is contains services         * that should remain running.         */        public static final int IMPORTANCE_SERVICE = 300;                /**         * Constant for {@link #importance}: this process process contains         * background code that is expendable.         */        public static final int IMPORTANCE_BACKGROUND = 400;                /**         * Constant for {@link #importance}: this process is empty of any         * actively running code.         */        public static final int IMPORTANCE_EMPTY = 500;  

需要權限:

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

具體操作代碼如下:

package com.example.demo;  import java.util.List;  import android.app.Activity; import android.app.ActivityManager; import android.app.ActivityManager.MemoryInfo; import android.app.ActivityManager.RunningAppProcessInfo; import android.content.Context; import android.content.pm.PackageManager; import android.content.pm.PackageManager.NameNotFoundException; import android.os.Bundle; import android.util.Log; import android.view.Menu; import android.view.MenuItem; import android.view.View; import android.widget.Toast;  public class CleanProcessActivity extends Activity {    private static final String TAG = "Clean";    @Override   protected void onCreate(Bundle savedInstanceState) {     super.onCreate(savedInstanceState);     setContentView(R.layout.activity_clean_process);   }   public void clean(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(this);      Log.d(TAG, "-----------before memory info : " + beforeMem);      int count = 0;      PackageManager pm = getPackageManager();          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 得到該進程下運行的包名              String appName = null;             try {               appName = (String) pm.getApplicationLabel(pm.getApplicationInfo(pkgList[j], 0));              } catch (NameNotFoundException e) {               // TODO Auto-generated catch block               e.printStackTrace();             }             Log.d(TAG, "It will be killed, package name : " + pkgList[j]+" -- "+appName );              am.killBackgroundProcesses(pkgList[j]);              count++;            }          }         }      }       long afterMem = getAvailMemory(this);      Log.d(TAG, "----------- after memory info : " + afterMem);      Toast.makeText(this, "clear " + count + " process, "            + (afterMem - beforeMem) + "M", Toast.LENGTH_LONG).show();    }   private long getAvailMemory(CleanProcessActivity cleanProcessActivity) {     // 獲取android當前可用內存大小      ActivityManager am = (ActivityManager) 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);    }  } 

注意:

我這里選擇閾值是IMPORTANCE_VISIBLE級別的,也就是非可見的后臺進程和服務會被殺掉(一些系統進程肯定除外)。
清理的效果跟金山清理大師和360桌面的一鍵清理效果差不多。

如果不想殺的太兇,可以選擇IMPORTANCE_SERVICE級別,殺掉那些長時間沒用或者空進程了,這個級別的清理力度不夠大,達不到金山清理大師的效果。

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持武林網。

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 富阳市| 安平县| 萨迦县| 桂东县| 枝江市| 资中县| 金华市| 南江县| 库伦旗| 自治县| 清河县| 偏关县| 安溪县| 吉林省| 镇巴县| 沈丘县| 仙居县| 长寿区| 定远县| 隆化县| 聂拉木县| 榆林市| 邳州市| 营山县| 化隆| 山丹县| 禹城市| 巴塘县| 平山县| 浑源县| 西宁市| 绥棱县| 阿拉善盟| 长治县| 蓬溪县| 新乡县| 嘉峪关市| 法库县| 上犹县| 淮滨县| 措勤县|