国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 系統 > Android > 正文

Android編程實現獲取系統內存、CPU使用率及狀態欄高度的方法示例

2019-12-12 02:10:14
字體:
來源:轉載
供稿:網友

本文實例講述了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;  }  /**   * 用于獲取狀態欄的高度。   *   * @return 返回狀態欄高度的像素值。   */  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;  }  /**   * 計算已使用內存的百分比,并返回。   *   * @param context   *      可傳入應用程序上下文。   * @return 已使用內存的百分比,以字符串形式返回。   */  public static String getUsedPercentValue(Context context) {    long totalMemorySize = getTotalMemory();    long availableSize = getAvailableMemory(context) / 1024;    int percent = (int) ((totalMemorySize - availableSize) / (float) totalMemorySize * 100);    return percent + "%";  }  /**   * 獲取當前可用內存,返回數據以字節為單位。   *   * @param context 可傳入應用程序上下文。   * @return 當前可用內存。   */  public static long getAvailableMemory(Context context) {    ActivityManager.MemoryInfo mi = new ActivityManager.MemoryInfo();    getActivityManager(context).getMemoryInfo(mi);    return mi.availMem;  }  /**   * 獲取系統總內存,返回字節單位為KB   * @return 系統總內存   */  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("http://D+", ""));    } catch (IOException e) {      e.printStackTrace();    }    return totalMemorySize;  }  /**   * 獲取頂層activity的包名   * @param context   * @return activity的包名   */  public static String getTopActivityPackageName(Context context) {    ActivityManager activityManager = getActivityManager(context);    List<RunningTaskInfo> runningTasks = activityManager.getRunningTasks(1);    return runningTasks.get(0).topActivity.getPackageName();  }  /**   * 獲取當前進程的CPU使用率   * @return CPU的使用率   */  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;  }  /**   * 獲取總的CPU使用率   * @return CPU使用率   */  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;  }  /**   * 獲取系統總CPU使用時間   * @return 系統CPU總的使用時間   */  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();  }  /**   * 獲取當前進程的CPU使用時間   * @return 當前進程的CPU使用時間   */  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資源操作技巧匯總》、《Android視圖View技巧總結》、《Android操作XML數據技巧總結》、《Android編程之activity操作技巧總結》、《Android文件操作技巧匯總》、《Android操作json格式數據技巧總結》、《Android開發入門與進階教程》及《Android控件用法總結

希望本文所述對大家Android程序設計有所幫助。

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 上栗县| 隆昌县| 崇左市| 西丰县| 阿瓦提县| 肥乡县| 江孜县| 汽车| 托里县| 阿拉善盟| 桃园市| 龙州县| 收藏| 鄯善县| 孝感市| 乌拉特后旗| 黄大仙区| 毕节市| 石城县| 华蓥市| 阿鲁科尔沁旗| 齐河县| 武威市| 怀安县| 建湖县| 江门市| 兴文县| 潢川县| 夏邑县| 嘉定区| 本溪市| 稷山县| 玉林市| 临海市| 施甸县| 顺平县| 红安县| 陆良县| 金阳县| 大洼县| 临城县|