package com.itheima.mobileguard.utils;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.InputStreamReader;
import java.util.List;
import android.R.integer;
import android.app.ActivityManager;
import android.app.ActivityManager.MemoryInfo;
import android.app.ActivityManager.RunningAppProcessInfo;
import android.app.ActivityManager.RunningServiceInfo;
import android.content.Context;
public class SystemInfoUtils {
/**
* 判断一个服务是否处于运行状态
*
* @param context
* 上下文
* @return
*/
public static boolean isServiceRunning(Context context, String className) {
ActivityManager am = (ActivityManager) context
.getSystemService(Context.ACTIVITY_SERVICE);
List<RunningServiceInfo> infos = am.getRunningServices(200);
for (RunningServiceInfo info : infos) {
String serviceClassName = info.service.getClassName();
if (className.equals(serviceClassName)) {
return true;
}
}
return false;
}
/**
* 返回进程的总个数
*
* @param context
* @return
*/
public static int getProcessCount(Context context) {
ActivityManager activityManager = (ActivityManager) context
.getSystemService(context.ACTIVITY_SERVICE);
List<RunningAppProcessInfo> runningAppProcesses = activityManager
.getRunningAppProcesses();
return runningAppProcesses.size();
}
public static long getAvailMem(Context context) {
ActivityManager activityManager = (ActivityManager) context
.getSystemService(context.ACTIVITY_SERVICE);
MemoryInfo memoryInfo = new ActivityManager.MemoryInfo();
activityManager.getMemoryInfo(memoryInfo);
return memoryInfo.availMem;
}
public static long getTotalMem(Context context) {
try {
FileInputStream fis = new FileInputStream(new File("/proc/meminfo"));
BufferedReader reader = new BufferedReader(new InputStreamReader(
fis));
String readLine = reader.readLine();
StringBuffer sb = new StringBuffer();
for (char c : readLine.toCharArray()) {
if (c >= '0' && c <= '9') {
sb.append(c);
}
}
return Long.parseLong(sb.toString()) * 1024;
} catch (Exception e) {
e.printStackTrace();
}
return 0;
}
}