Android如何獲取當(dāng)前CPU頻率和占用率
最近在優(yōu)化 App 的性能,需要獲取當(dāng)前 CPU視頻頻率和占用率,通過查詢資料,大致思路如下:
目前沒有標準的 API 來獲取 CPU 的使用頻率,只能通過讀取指定 CPU 文件獲取當(dāng)前 CPU 頻率,在某些機器或者特定版本中,可能需要ROOT 權(quán)限或者特殊權(quán)限,因此會存在一定幾率的失敗,因此需要做好 Try…catch 動作。又因為現(xiàn)在手機 CPU 的多核數(shù)目,因此我們可能需要獲取多個 CPU 頻率數(shù),并取平均值。
獲取系統(tǒng) CPU 核心數(shù):
val cpuCoreNum = Runtime.getRuntime().availableProcessors()
獲取指定 CPU 當(dāng)前頻率:
/sys/devices/system/cpu/cpu${index}/cpufreq/scaling_cur_freq
那么核心代碼為:
private fun getAllCpuCoreFrequency() : Long {
var frequency = 0L
for (index in 0 until cpuCoreNum){
frequency += readFile("/sys/devices/system/cpu/cpu$index/cpufreq/scaling_cur_freq")
}
BLog.d("frequency : $frequency")
return frequency / cpuCoreNum
}
private fun readFile(filePath: String): Long{
try {
val file = RandomAccessFile(filePath, "r")
val content = file.readLine()
file.close()
if (TextUtils.isEmpty(content)){
return 0L
}
BLog.d("readFile content : $content")
return content.trim().toLong()
}catch (e : Exception){
e.printStackTrace()
return 0L
}
}如果需要獲取 CPU 的占用率,那么就需要知道每個核心的最大頻率和最小頻率,同樣是通過文件獲?。?/p>
//max frequency file
/sys/devices/system/cpu/cpu${index}/cpufreq/cpuinfo_max_freq
???????//min frequency file
/sys/devices/system/cpu/cpu${index}/cpufreq/cpuinfo_min_freq
那么核心代碼為:
object CPUUtils {
private var cpuCoreNum = 0
private var cpuMaxFrequency = 0L
private var cpuMinFrequency = 0L
fun initCpuCoreNum(){
if (cpuCoreNum <= 0 || cpuMaxFrequency <= 0L || cpuMinFrequency <= 0L){
cpuCoreNum = Runtime.getRuntime().availableProcessors()
initMaxAndMinFrequency()
if (cpuCoreNum > 0 && cpuMaxFrequency > 0L && cpuMinFrequency > 0L){
SpManager.getInstance().setCanUseCPUFrequency(true)
}
}
BLog.d("cpuCoreNum : $cpuCoreNum")
}
private fun initMaxAndMinFrequency() {
if (cpuCoreNum <= 0){
return
}
cpuMaxFrequency = 0L
cpuMinFrequency = 0L
for (index in 0 until cpuCoreNum){
cpuMaxFrequency += readFile("/sys/devices/system/cpu/cpu${index}/cpufreq/cpuinfo_max_freq")
cpuMinFrequency += readFile("/sys/devices/system/cpu/cpu${index}/cpufreq/cpuinfo_min_freq")
}
BLog.d("cpuMaxFrequency : $cpuMaxFrequency, cpuMinFrequency : $cpuMinFrequency")
}
private fun readFile(filePath: String): Long{
try {
val file = RandomAccessFile(filePath, "r")
val content = file.readLine()
file.close()
if (TextUtils.isEmpty(content)){
return 0L
}
BLog.d("readFile content : $content")
return content.trim().toLong()
}catch (e : Exception){
ExceptionHandler.recordException(e)
return 0L
}
}
private fun getAllCpuCoreFrequency() : Long {
initCpuCoreNum()
if (cpuCoreNum <=0){
return 0L
}
var frequency = 0L
for (index in 0 until cpuCoreNum){
frequency += readFile("/sys/devices/system/cpu/cpu$index/cpufreq/scaling_cur_freq")
}
BLog.d("frequency : $frequency")
return frequency
}
fun findCurrentFrequencyPercent() : Long {
val currentFrequency = getAllCpuCoreFrequency()
BLog.d("currentFrequency : $currentFrequency, cpuMinFrequency : $cpuMinFrequency, cpuMaxFrequency : $cpuMaxFrequency")
if (cpuMaxFrequency - cpuMinFrequency <= 0L || currentFrequency - cpuMinFrequency < 0L || cpuMaxFrequency - currentFrequency < 0L){
return 0L
}
return (currentFrequency - cpuMinFrequency) * 100 / (cpuMaxFrequency - cpuMinFrequency)
}
fun getCpuCoreFrequency() : Long {
initCpuCoreNum()
if (cpuCoreNum <=0){
return 0L
}
return getAllCpuCoreFrequency() / cpuCoreNum
}
}
獲取 CPU 頻率:
CPUUtils.getCpuCoreFrequency()
獲取 CPU 占用率:
CPUtils.findCurrentFrequencyPercent()
到此這篇關(guān)于Android如何獲取當(dāng)前CPU頻率和占用率的文章就介紹到這了,更多相關(guān)Android獲取CPU頻率和占用率內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Android百度定位導(dǎo)航之基于百度地圖移動獲取位置和自動定位
項目需求是這樣的,首先定位我當(dāng)前的起始位置,并跟隨移動不斷自動定位我的當(dāng)前位置,下面通過本文給大家介紹android百度定位導(dǎo)航之基于百度地圖移動獲取位置和自動定位,需要的朋友參考下2016-01-01
Android應(yīng)用開發(fā)中使用Fragment的入門學(xué)習(xí)教程
這篇文章主要介紹了Android應(yīng)用開發(fā)中Fragment的入門學(xué)習(xí)教程,可以把Fragment看作為Activity基礎(chǔ)之上的模塊,需要的朋友可以參考下2016-02-02
Kotlin語言使用BroadcastReceiver示例介紹
Android開發(fā)的四大組件分別是:活動(activity),用于表現(xiàn)功能;服務(wù)(service),后臺運行服務(wù),不提供界面呈現(xiàn);廣播接受者(Broadcast Receive),勇于接收廣播;內(nèi)容提供者(Content Provider),支持多個應(yīng)用中存儲和讀取數(shù)據(jù),相當(dāng)于數(shù)據(jù)庫,本篇著重介紹廣播組件2022-09-09
Android+Flutter實現(xiàn)文字跑馬燈特效
跑馬燈常常被運用在很多領(lǐng)域, 例如商場的電子條幅、大樓的宣傳廣告位、地鐵的廣告位。今天我們來說一下flutter 怎么通過繪制來實現(xiàn)跑馬燈效果!,希望對大家有所幫助2022-11-11
協(xié)程作用域概念迭代RxTask?實現(xiàn)自主控制
這篇文章主要為大家介紹了協(xié)程作用域概念迭代RxTask實現(xiàn)自主控制詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2022-10-10
仿iPhone風(fēng)格對話框(附件包含例子/jar包/jar包源碼)
這個對框完全繼承、仿照AlertDialog,只是實現(xiàn)了自定義效果;另外,沒有實現(xiàn)setIcon,因為iphone中的對話框多數(shù)都沒有圖標;附件包含例子、jar包、jar包源碼2013-01-01
Android編程實現(xiàn)改變控件背景及形態(tài)的方法
這篇文章主要介紹了Android編程實現(xiàn)改變控件背景及形態(tài)的方法,涉及Android控件布局設(shè)置的相關(guān)技巧,需要的朋友可以參考下2016-02-02

