獲取android手機的CPU的使用率

2021-08-27 20:33:09 字數 1580 閱讀 3928

/**

* 通過獲取cpu一行的資料,即可進行cpu佔用率的計算。我們會用到的資料有:

- user(21441),從系統啟動開始累計到當前時刻,處於使用者態的執行時間,不包含nice值為負的程序。

- nice(3634),從系統啟動開始累計到當前時刻,nice值為負的程序所占用的cpu時間。

- system(13602),從系統啟動開始累計到當前時刻,處於核心態的執行時間。

- idle(818350),從系統啟動開始累計到當前時刻,除io等待時間以外的其它等待時間。

- iowait(3535),從系統啟動開始累計到當前時刻,io等待時間。

- irq(2),從系統啟動開始累計到當前時刻,硬中斷時間。

- softirq(99),從系統啟動開始累計到當前時刻,軟中斷時間。

總的cpu佔用率的計算方法為:取樣兩個足夠短的時間間隔的cpu快照,

cpu佔用率 = 100*((totaltime2-totaltime1)-(idle2-idle1))/(totaltime2-totaltime1)。

* */

//獲取cpu使用率

public float getcpurate() catch (interruptedexception e)

//取樣第二次cpu資訊快照

mapmap2 = getmap();

long totaltime2 = gettime(map2);

system.out.println(totaltime2+"............................totaltime2");

//獲取idletime1

long idletime2 = long.parselong(map2.get("idle"));

system.out.println(idletime2+"................idletime2");

//得到cpu的使用率

float cpurate = 100*((totaltime2-totaltime1)-(idletime2-idletime1))/(totaltime2-totaltime1);

return cpurate;

}//得到cpu資訊

public long gettime(mapmap)

//取樣cpu資訊快照的函式,返回map型別

public mapgetmap() catch (filenotfoundexception e) catch (ioexception e)

mapmap = new hashmap<>();

map.put("user",cpuinfos[2]);

map.put("nice",cpuinfos[3]);

map.put("system",cpuinfos[4]);

map.put("idle",cpuinfos[5]);

map.put("iowait",cpuinfos[6]);

map.put("irq",cpuinfos[7]);

map.put("softirq",cpuinfos[8]);

return map;

}

Android中獲取手機cpu序列號

這一篇 主要講的的是android裡獲取手機cpu序列號,很少,也應該算很簡單了。下面來看看 獲取cpu序列號 return cpu序列號 16位 讀取失敗為 0000000000000000 public static string getcpuserial string str strcpu c...

Android如何實現獲取手機CPU的溫度

在做專案過程中,有時需要獲取手機cpu的溫度。目前市面上常見的cpu主要有兩種 mtk 聯發科 qualcomm 高通 當然還有我們華為的海思麒麟cpu,以及三星的cpu。後兩種cpu在本篇文章中就不做展開,有興趣的同學,可以自行去研究研究。1 開啟終端命令視窗,如windows下的cmd程式。2 ...

Android手機對應的CPU型號

寫這篇文章主要是由於我們移動端測試android測試機比較多,有時候為了找某一款cpu型號的手機必須要把所有手機查一遍,為了以後查詢更方便快捷以及定位手機效能,現總結如下 ps 最近發現總結很重要 測試機 cpu型號 小公尺5高通 驍龍820 nexus 6p 高通 驍龍810 nexus 6 高通...