一区二区三区日韩精品-日韩经典一区二区三区-五月激情综合丁香婷婷-欧美精品中文字幕专区

分享

進程的信息(如圖標,內(nèi)存)獲取和殺死,進程和服務(wù)是否運行的判斷

 Dragon_chen 2016-09-15
public class ProgressInfoUtil {
//獲取進程總數(shù)
public static int getProgressCount(Context context) {
ActivityManager activityManager = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
List<ActivityManager.RunningAppProcessInfo> runningAppProcessInfos = activityManager.getRunningAppProcesses();
return runningAppProcessInfos.size();
}

/*
返回可用內(nèi)存
*/
public static long getAvailSpace(Context context) {
ActivityManager activityManager = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
ActivityManager.MemoryInfo memoryInfo = new ActivityManager.MemoryInfo();
activityManager.getMemoryInfo(memoryInfo);
return memoryInfo.availMem;
}

/*
返回總共內(nèi)存
*/
public static long getTotalSpace(Context context) {
//totalMem最小版本是16
// ActivityManager activityManager = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
// ActivityManager.MemoryInfo memoryInfo = new ActivityManager.MemoryInfo();
// activityManager.getMemoryInfo(memoryInfo);
// return memoryInfo.totalMem;
BufferedReader bufferedReader = null;
FileReader fileReader = null;
try {
fileReader = new FileReader("proc/meminfo");
bufferedReader = new BufferedReader(fileReader);
String lineOne = bufferedReader.readLine();
char[] charArray = lineOne.toCharArray();
StringBuilder stringBuilder = new StringBuilder();
for (char c : charArray) {
if (c >= '0' && c <= '9')
stringBuilder.append(c);
}
return Long.parseLong(stringBuilder.toString()) * 1024;
} catch (Exception e) {
e.printStackTrace();
} finally {
if (fileReader != null && bufferedReader != null) {
try {
fileReader.close();
bufferedReader.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
return 0;
}

/*
返回運行應(yīng)用信息
*/
public static ArrayList<ProgressInfoBean> getProgressInfo(Context context) {
ArrayList<ProgressInfoBean> progressInfoBeen = new ArrayList<ProgressInfoBean>();
ActivityManager activityManager = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
PackageManager packageManager = context.getPackageManager();
List<ActivityManager.RunningAppProcessInfo> list = activityManager.getRunningAppProcesses();
//通過RunningAppProcessInfo和packageManager獲取進程相關(guān)信息,名稱,包名,所占內(nèi)存
for (ActivityManager.RunningAppProcessInfo r : list) {
ProgressInfoBean progressInfoBean = new ProgressInfoBean();
//獲取包名
progressInfoBean.packageName = r.processName;
//獲取占用內(nèi)存的大小
Debug.MemoryInfo[] memoryInfo = activityManager.getProcessMemoryInfo(new int[]{r.pid});
progressInfoBean.memize = memoryInfo[0].getTotalPrivateDirty() * 1024;
try {
//獲取應(yīng)用圖標和名稱
ApplicationInfo applicationInfo = packageManager.getApplicationInfo(progressInfoBean.packageName, 0);
progressInfoBean.icon = applicationInfo.loadIcon(packageManager);
progressInfoBean.name = applicationInfo.loadLabel(packageManager).toString();
if ((applicationInfo.flags & ApplicationInfo.FLAG_SYSTEM) == ApplicationInfo.FLAG_SYSTEM) {
progressInfoBean.isSystem = true;
} else {
progressInfoBean.isSystem = false;
}
} catch (PackageManager.NameNotFoundException e) {
e.printStackTrace();
//異常處理
progressInfoBean.icon = context.getResources().getDrawable(R.mipmap.ic_launcher);
progressInfoBean.name = r.processName;
progressInfoBean.isSystem = true;
}
progressInfoBeen.add(progressInfoBean);
}
return progressInfoBeen;
}
//殺死進程
public static void killProcess(Context context,ProgressInfoBean pb)
{
ActivityManager activityManager = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
//殺進程要加權(quán)限,kill background process權(quán)限
activityManager.killBackgroundProcesses(pb.packageName);
}
//殺死所有進程
public static void killAllProcess(Context context)
{
ActivityManager activityManager = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
List<ActivityManager.RunningAppProcessInfo> runningAppProcessInfos = activityManager.getRunningAppProcesses();
for(ActivityManager.RunningAppProcessInfo r : runningAppProcessInfos) {
if(r.processName.equals(context.getPackageName())) {
}
else {
activityManager.killBackgroundProcesses(r.processName);
}
}
}
//檢查服務(wù)是否在運行
public static boolean serviceIsRunning(Context ctx, String serviceName){
//1,獲取activityMananger管理者對象,可以去獲取當前手機正在運行的所有服務(wù)
ActivityManager mAM = (ActivityManager) ctx.getSystemService(Context.ACTIVITY_SERVICE);
//2,獲取手機中正在運行的服務(wù)集合(多少個服務(wù))
List<ActivityManager.RunningServiceInfo> runningServices = mAM.getRunningServices(1000);
//3,遍歷獲取的所有的服務(wù)集合,拿到每一個服務(wù)的類的名稱,和傳遞進來的類的名稱作比對,如果一致,說明服務(wù)正在運行
for (ActivityManager.RunningServiceInfo runningServiceInfo : runningServices) {
//4,獲取每一個真正運行服務(wù)的名稱
if(serviceName.equals(runningServiceInfo.service.getClassName())){
return true;
}
}
return false;
}
//檢查程序是否在運行
public static boolean activityIsRunning(Context context,String packageName)
{
ActivityManager am = (ActivityManager)context.getSystemService(Context.ACTIVITY_SERVICE);
List<ActivityManager.RunningTaskInfo> list = am.getRunningTasks(100);
for (ActivityManager.RunningTaskInfo info : list) {
if (info.topActivity.getPackageName().equals(packageName) || info.baseActivity.getPackageName().equals(packageName)) {
return true;
}
}
return false;
}

    本站是提供個人知識管理的網(wǎng)絡(luò)存儲空間,所有內(nèi)容均由用戶發(fā)布,不代表本站觀點。請注意甄別內(nèi)容中的聯(lián)系方式、誘導購買等信息,謹防詐騙。如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請點擊一鍵舉報。
    轉(zhuǎn)藏 分享 獻花(0

    0條評論

    發(fā)表

    請遵守用戶 評論公約

    類似文章 更多

    高清国产日韩欧美熟女| 国产一区二区三区精品免费| 少妇人妻一级片一区二区三区| 亚洲男人的天堂就去爱| 免费国产成人性生活生活片| 成人免费在线视频大香蕉| 亚洲欧洲日韩综合二区| 亚洲欧美日韩中文字幕二欧美 | 欧美精品女同一区二区| 美日韩一区二区精品系列| 中文字幕亚洲视频一区二区| 国产综合欧美日韩在线精品| 国产成人高清精品尤物| 高清国产日韩欧美熟女| 欧美一区二区三区喷汁尤物| 九九热这里只有精品视频| 国产欧美一区二区久久| 天堂热东京热男人天堂| 久热99中文字幕视频在线| 国产精品人妻熟女毛片av久| 国产成人精品国产亚洲欧洲| 最新日韩精品一推荐日韩精品| 久草视频在线视频在线观看| 亚洲欧美日韩网友自拍| 日韩国产精品激情一区| 日本免费熟女一区二区三区| 免费性欧美重口味黄色| 中国少妇精品偷拍视频 | 国产又粗又猛又黄又爽视频免费 | 国产精品一级香蕉一区| 国产一区二区三区av在线| 国产精品视频一区麻豆专区| 日韩一区二区三区久久| 成年午夜在线免费视频| 夜夜嗨激情五月天精品| 国产精品一区二区丝袜| 国产精品一区二区高潮| 国产成人亚洲综合色就色| 中文字幕一区二区熟女| 日本精品中文字幕在线视频| 国产精品免费福利在线|