Android編程實(shí)現(xiàn)獲取系統(tǒng)內(nèi)存、CPU使用率及狀態(tài)欄高度的方法示例
本文實(shí)例講述了Android編程實(shí)現(xiàn)獲取系統(tǒng)內(nèi)存、CPU使用率及狀態(tài)欄高度的方法。分享給大家供大家參考,具體如下:
DeviceInfoManage類(lèi)用于獲取系統(tǒng)的內(nèi)存,CPU的信息,以及狀態(tài)欄的高度
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;
}
/**
* 用于獲取狀態(tài)欄的高度。
*
* @return 返回狀態(tài)欄高度的像素值。
*/
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;
}
/**
* 計(jì)算已使用內(nèi)存的百分比,并返回。
*
* @param context
* 可傳入應(yīng)用程序上下文。
* @return 已使用內(nèi)存的百分比,以字符串形式返回。
*/
public static String getUsedPercentValue(Context context) {
long totalMemorySize = getTotalMemory();
long availableSize = getAvailableMemory(context) / 1024;
int percent = (int) ((totalMemorySize - availableSize) / (float) totalMemorySize * 100);
return percent + "%";
}
/**
* 獲取當(dāng)前可用內(nèi)存,返回?cái)?shù)據(jù)以字節(jié)為單位。
*
* @param context 可傳入應(yīng)用程序上下文。
* @return 當(dāng)前可用內(nèi)存。
*/
public static long getAvailableMemory(Context context) {
ActivityManager.MemoryInfo mi = new ActivityManager.MemoryInfo();
getActivityManager(context).getMemoryInfo(mi);
return mi.availMem;
}
/**
* 獲取系統(tǒng)總內(nèi)存,返回字節(jié)單位為KB
* @return 系統(tǒng)總內(nèi)存
*/
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();
//將非數(shù)字的字符替換為空
totalMemorySize = Integer.parseInt(subMemoryLine.replaceAll("\\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();
}
/**
* 獲取當(dāng)前進(jìn)程的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;
}
/**
* 獲取系統(tǒng)總CPU使用時(shí)間
* @return 系統(tǒng)CPU總的使用時(shí)間
*/
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();
}
/**
* 獲取當(dāng)前進(jìn)程的CPU使用時(shí)間
* @return 當(dāng)前進(jìn)程的CPU使用時(shí)間
*/
public static long getAppCpuTime()
{
// 獲取應(yīng)用占用的CPU時(shí)間
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);
}
}
}
更多關(guān)于Android相關(guān)內(nèi)容感興趣的讀者可查看本站專(zhuān)題:《Android資源操作技巧匯總》、《Android視圖View技巧總結(jié)》、《Android操作XML數(shù)據(jù)技巧總結(jié)》、《Android編程之a(chǎn)ctivity操作技巧總結(jié)》、《Android文件操作技巧匯總》、《Android操作json格式數(shù)據(jù)技巧總結(jié)》、《Android開(kāi)發(fā)入門(mén)與進(jìn)階教程》及《Android控件用法總結(jié)》
希望本文所述對(duì)大家Android程序設(shè)計(jì)有所幫助。
- 解決Android 高CPU占用率的問(wèn)題
- Android、Flutter為不同的CPU架構(gòu)包打包APK(v7a、v8a、x86區(qū)別)
- Python獲取android設(shè)備cpu和內(nèi)存占用情況
- 淺析AndroidStudio3.0最新 Android Profiler分析器(cpu memory network 分析器)
- Android獲取設(shè)備CPU核數(shù)、時(shí)鐘頻率以及內(nèi)存大小的方法
- 解析Android獲取系統(tǒng)cpu信息,內(nèi)存,版本,電量等信息的方法詳解
- android獲取手機(jī)cpu并判斷是單核還是多核
- Android 輕松獲取CPU型號(hào)的方法
相關(guān)文章
Android入門(mén)之彈出式對(duì)話框的實(shí)現(xiàn)
Android Studio里有一種Dialog叫PopWindow,它是一種“可阻塞式Dialog”,即彈出后除非你給它一個(gè)“動(dòng)作”否則就一直顯示在那。本文就將實(shí)現(xiàn)這樣的彈出式對(duì)話框,感興趣的可以了解一下2022-11-11
Android中創(chuàng)建快捷方式代碼實(shí)例
這篇文章主要介紹了Android中創(chuàng)建快捷方式代碼實(shí)例,本文分為三個(gè)步驟實(shí)現(xiàn),并分別給出對(duì)應(yīng)實(shí)現(xiàn)代碼,需要的朋友可以參考下2015-04-04
Android實(shí)現(xiàn)使用流媒體播放遠(yuǎn)程mp3文件的方法
這篇文章主要介紹了Android實(shí)現(xiàn)使用流媒體播放遠(yuǎn)程mp3文件的方法,結(jié)合實(shí)例形式分析了Android遠(yuǎn)程播放音頻文件的相關(guān)步驟與實(shí)現(xiàn)技巧,需要的朋友可以參考下2016-08-08
淺談android獲取存儲(chǔ)目錄(路徑)的幾種方式和注意事項(xiàng)
今天小編就為大家分享一篇淺談android獲取存儲(chǔ)目錄(路徑)的幾種方式和注意事項(xiàng),具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2018-08-08
Android Root設(shè)備中的su權(quán)限獲取和使用詳解
本篇文章主要介紹了Android Root設(shè)備中的su權(quán)限獲取和使用詳解,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2018-01-01
Android編程簡(jiǎn)單實(shí)現(xiàn)九宮格示例
這篇文章主要介紹了Android編程簡(jiǎn)單實(shí)現(xiàn)九宮格,結(jié)合具體實(shí)例形式分析了Android實(shí)現(xiàn)九宮格的具體步驟與相關(guān)布局、功能實(shí)現(xiàn)技巧,需要的朋友可以參考下2017-06-06
Android?ButterKnife依賴(lài)注入框架使用教程
ButterKnife是一個(gè)專(zhuān)注于Android系統(tǒng)的View注入框架,以前總是要寫(xiě)很多findViewById來(lái)找到View對(duì)象,有了ButterKnife可以很輕松的省去這些步驟。是大神JakeWharton的力作,目前使用很廣2023-02-02
Android入門(mén)之ViewFlipper翻轉(zhuǎn)視圖的使用詳解
本篇給大家?guī)Я说氖荲iewFlipper,它是Android自帶的一個(gè)多頁(yè)面管理控件,且可以自動(dòng)播放!本篇我們我們會(huì)使用兩個(gè)例子:一個(gè)自動(dòng)播放首頁(yè)輪播頁(yè)一個(gè)手動(dòng)可左右滑動(dòng)道頁(yè)的輪播頁(yè)來(lái)說(shuō)透這個(gè)組件的使用,感興趣的可以了解一下2022-11-11
Android 網(wǎng)絡(luò)html源碼查看器詳解及實(shí)例
這篇文章主要介紹了Android 網(wǎng)絡(luò)html源碼查看器詳解及實(shí)例的相關(guān)資料,需要的朋友可以參考下2017-03-03

