1.判断指定的service是否在运行
1 public static boolean isServiceRunning(Context ctx, String serviceName) { 2 ActivityManager am = (ActivityManager) ctx 3 .getSystemService(Context.ACTIVITY_SERVICE); 4 List< RunningServiceInfo > runningServices = am.getRunningServices(200); // 获取运行的所有service,100一般可以获取到所有的service. 5 for ( RunningServiceInfo runningServiceInfo : runningServices ) { 6 String className = runningServiceInfo.service.getClassName(); //获取到运行服务的类名 7 if ( className.equals(serviceName) ) { // 假如在运行的service中含有传入的service名称,返回true 8 return true; 9 } 10 } 11 return false; 12 }
2.获取到系统运行的进程数
1 public static int getRunningTaskCount(Context context) { 2 ActivityManager am = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE); 3 List< ActivityManager.RunningAppProcessInfo > runningAppProcesses = am.getRunningAppProcesses(); 4 return runningAppProcesses.size(); 5 }
3.获取设备可用内存
1 public static long getAvaliMem(Context context) { 2 ActivityManager am = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE); 3 ActivityManager.MemoryInfo outInfo = new ActivityManager.MemoryInfo(); 4 am.getMemoryInfo(outInfo); 5 return outInfo.availMem; 6 }
4.获取设备总内存
1 public static long getTotalMem(Context context) { 2 ActivityManager am = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE); 3 ActivityManager.MemoryInfo outInfo = new ActivityManager.MemoryInfo(); 4 am.getMemoryInfo(outInfo); 5 return outInfo.totalMem; 6 }