Android编程如何实现获取系统内存、CPU使用率及状态栏高度的方法
短信预约 -IT技能 免费直播动态提醒
这篇文章将为大家详细讲解有关Android编程如何实现获取系统内存、CPU使用率及状态栏高度的方法,小编觉得挺实用的,因此分享给大家做个参考,希望大家阅读完这篇文章后可以有所收获。
本文实例讲述了Android编程实现获取系统内存、CPU使用率及状态栏高度的方法。分享给大家供大家参考,具体如下:
DeviceInfoManage类用于获取系统的内存,CPU的信息,以及状态栏的高度
import java.io.BufferedReader;import java.io.FileInputStream;import java.io.FileReader;import java.io.IOException;import java.io.InputStreamReader;import java.lang.reflect.Field;import java.util.List;import android.app.ActivityManager;import android.app.ActivityManager.RunningTaskInfo;import android.content.Context;public class DeviceInfoManager {// private static final String TAG = "DeviceInfoManager"; private static ActivityManager mActivityManager; public synchronized static ActivityManager getActivityManager(Context context) { if (mActivityManager == null) { mActivityManager = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE); } return mActivityManager; } public static int getStatusBarHeight(Context context) { int statusBarHeight = 0; try { Class<?> c = Class.forName("com.android.internal.R$dimen"); Object o = c.newInstance(); Field field = c.getField("status_bar_height"); int x = (Integer) field.get(o); statusBarHeight = context.getResources().getDimensionPixelSize(x); } catch (Exception e) { e.printStackTrace(); } return statusBarHeight; } public static String getUsedPercentValue(Context context) { long totalMemorySize = getTotalMemory(); long availableSize = getAvailableMemory(context) / 1024; int percent = (int) ((totalMemorySize - availableSize) / (float) totalMemorySize * 100); return percent + "%"; } public static long getAvailableMemory(Context context) { ActivityManager.MemoryInfo mi = new ActivityManager.MemoryInfo(); getActivityManager(context).getMemoryInfo(mi); return mi.availMem; } public static long getTotalMemory() { long totalMemorySize = 0; String dir = "/proc/meminfo"; try { FileReader fr = new FileReader(dir); BufferedReader br = new BufferedReader(fr, 2048); String memoryLine = br.readLine(); String subMemoryLine = memoryLine.substring(memoryLine.indexOf("MemTotal:")); br.close(); //将非数字的字符替换为空 totalMemorySize = Integer.parseInt(subMemoryLine.replaceAll("\\D+", "")); } catch (IOException e) { e.printStackTrace(); } return totalMemorySize; } public static String getTopActivityPackageName(Context context) { ActivityManager activityManager = getActivityManager(context); List<RunningTaskInfo> runningTasks = activityManager.getRunningTasks(1); return runningTasks.get(0).topActivity.getPackageName(); } public static float getCurProcessCpuRate() { float totalCpuTime1 = getTotalCpuTime(); float processCpuTime1 = getAppCpuTime(); try { Thread.sleep(360); } catch (Exception e) { } float totalCpuTime2 = getTotalCpuTime(); float processCpuTime2 = getAppCpuTime(); float cpuRate = 100 * (processCpuTime2 - processCpuTime1) / (totalCpuTime2 - totalCpuTime1); return cpuRate; } public static float getTotalCpuRate() { float totalCpuTime1 = getTotalCpuTime(); float totalUsedCpuTime1 = totalCpuTime1 - sStatus.idletime; try { Thread.sleep(360); } catch (InterruptedException e) { e.printStackTrace(); } float totalCpuTime2 = getTotalCpuTime(); float totalUsedCpuTime2 = totalCpuTime2 - sStatus.idletime; float cpuRate = 100 * (totalUsedCpuTime2 - totalUsedCpuTime1) / (totalCpuTime2 - totalCpuTime1); return cpuRate; } public static long getTotalCpuTime() { String[] cpuInfos = null; try { BufferedReader reader = new BufferedReader(new InputStreamReader( new FileInputStream("/proc/stat")), 1000); String load = reader.readLine(); reader.close(); cpuInfos = load.split(" "); } catch (IOException ex) { ex.printStackTrace(); }// long totalCpu = Long.parseLong(cpuInfos[2])// + Long.parseLong(cpuInfos[3]) + Long.parseLong(cpuInfos[4])// + Long.parseLong(cpuInfos[6]) + Long.parseLong(cpuInfos[5])// + Long.parseLong(cpuInfos[7]) + Long.parseLong(cpuInfos[8]); sStatus.usertime = Long.parseLong(cpuInfos[2]); sStatus.nicetime = Long.parseLong(cpuInfos[3]); sStatus.systemtime = Long.parseLong(cpuInfos[4]); sStatus.idletime = Long.parseLong(cpuInfos[5]); sStatus.iowaittime = Long.parseLong(cpuInfos[6]); sStatus.irqtime = Long.parseLong(cpuInfos[7]); sStatus.softirqtime = Long.parseLong(cpuInfos[8]); return sStatus.getTotalTime(); } public static long getAppCpuTime() { // 获取应用占用的CPU时间 String[] cpuInfos = null; try { int pid = android.os.Process.myPid(); BufferedReader reader = new BufferedReader(new InputStreamReader( new FileInputStream("/proc/" + pid + "/stat")), 1000); String load = reader.readLine(); reader.close(); cpuInfos = load.split(" "); } catch (IOException ex) { ex.printStackTrace(); } long appCpuTime = Long.parseLong(cpuInfos[13]) + Long.parseLong(cpuInfos[14]) + Long.parseLong(cpuInfos[15]) + Long.parseLong(cpuInfos[16]); return appCpuTime; } static Status sStatus = new Status(); static class Status { public long usertime; public long nicetime; public long systemtime; public long idletime; public long iowaittime; public long irqtime; public long softirqtime; public long getTotalTime() { return (usertime + nicetime + systemtime + idletime + iowaittime + irqtime + softirqtime); } }}
Android是什么
Android是一种基于Linux内核的自由及开放源代码的操作系统,主要使用于移动设备,如智能手机和平板电脑,由美国Google公司和开放手机联盟领导及开发。
关于“Android编程如何实现获取系统内存、CPU使用率及状态栏高度的方法”这篇文章就分享到这里了,希望以上内容可以对大家有一定的帮助,使各位可以学到更多知识,如果觉得文章不错,请把它分享出去让更多的人看到。
免责声明:
① 本站未注明“稿件来源”的信息均来自网络整理。其文字、图片和音视频稿件的所属权归原作者所有。本站收集整理出于非商业性的教育和科研之目的,并不意味着本站赞同其观点或证实其内容的真实性。仅作为临时的测试数据,供内部测试之用。本站并未授权任何人以任何方式主动获取本站任何信息。
② 本站未注明“稿件来源”的临时测试数据将在测试完成后最终做删除处理。有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341