android系统开发之优雅的杀后台
public void clean(){
//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_SERVICE) {
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() {
// 获取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);
}
public float getFreeMemory(){
float memory = 0;
MemoryInfo info = new MemoryInfo();
mActivityManager.getMemoryInfo(info);
memory = info.availMem/1024/1024;
return memory;
}
public float getLocalMemory(){
float localMemory = 0;
String str1 = "/proc/meminfo";
String str2;
String[] arrayOfString;
long initial_memory = 0;
try {
FileReader fileReader = new FileReader(str1);
BufferedReader bufferedReader = new BufferedReader(fileReader, 8192);
str2 = bufferedReader.readLine();
arrayOfString = str2.split("\\s+");
initial_memory = Integer.valueOf(arrayOfString[1]);
bufferedReader.close();
} catch (Exception e) {
e.printStackTrace();
}
localMemory = (initial_memory / 1024);
return localMemory;
}
public int getPercentMemory(){
int memoryPercent = 0;
memoryPercent = (int) ((1-getFreeMemory()/getLocalMemory())*100);
if(memoryPercent < 0 || memoryPercent >100){
Log.e(TAG,"getPercentMemory Error memoryPercent == "+memoryPercent+" getFreeMemory() =="+getFreeMemory()+" getLocalMemory() == "+getLocalMemory());
memoryPercent = 0;
}
return memoryPercent;
}
IMPORTANCE_FOREGROUND,
IMPORTANCE_FOREGROUND_SERVICE,
IMPORTANCE_TOP_SLEEPING,
IMPORTANCE_VISIBLE,
IMPORTANCE_PERCEPTIBLE,
IMPORTANCE_CANT_SAVE_STATE,
IMPORTANCE_SERVICE,
IMPORTANCE_CACHED,
IMPORTANCE_GONE,
具体意思:ActivityManager.RunningAppProcessInfo
参考:https://blog.csdn.net/lzpdz/article/details/54646404?utm_source=blogxgwz4