最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Android獲取設(shè)備CPU核數(shù)、時(shí)鐘頻率以及內(nèi)存大小的方法

 更新時(shí)間:2016年07月25日 17:22:39   作者:feelang  
這篇文章主要介紹了Android獲取設(shè)備CPU核數(shù)、時(shí)鐘頻率以及內(nèi)存大小的方法,涉及Android針對(duì)系統(tǒng)硬件相關(guān)操作技巧,需要的朋友可以參考下

本文實(shí)例講述了Android獲取設(shè)備CPU核數(shù)、時(shí)鐘頻率以及內(nèi)存大小的方法。分享給大家供大家參考,具體如下:

因項(xiàng)目需要,分析了一下 Facebook 的開(kāi)源項(xiàng)目 - Device Year Class。

Device Year Class 的主要功能是根據(jù) CPU核數(shù)、時(shí)鐘頻率 以及 內(nèi)存大小 對(duì)設(shè)備進(jìn)行分級(jí)。代碼很簡(jiǎn)單,只包含兩個(gè)類(lèi):

DeviceInfo -> 獲取設(shè)備參數(shù),
YearClass -> 根據(jù)參數(shù)進(jìn)行分級(jí)。

下表是 Facebook 公司提供的分級(jí)標(biāo)準(zhǔn),其中 Year 欄表示分級(jí)結(jié)果。

Year Cores Clock RAM
2008 1 528MHz 192MB
2009 n/a 600MHz 290MB
2010 n/a 1.0GHz 512MB
2011 2 1.2GHz 1GB
2012 4 1.5GHz 1.5GB
2013 n/a 2.0GHz 2GB
2014 n/a >2GHz >2GB

關(guān)于輸出年份的計(jì)算方法可以參考源碼,本文只把一些比較常用的功能抽取出來(lái)做一個(gè)簡(jiǎn)要介紹。

獲取 CPU 核數(shù)

我們都知道,Linux 中的設(shè)備都是以文件的形式存在,CPU 也不例外,因此 CPU 的文件個(gè)數(shù)就等價(jià)與核數(shù)。

Android 的 CPU 設(shè)備文件位于 /sys/devices/system/cpu/ 目錄,文件名的的格式為 cpu\d+。

root@generic_x86_64:/sys/devices/system/cpu # ls
cpu0
cpufreq
cpuidle
kernel_max
modalias
offline
online
possible
power
present
uevent

統(tǒng)計(jì)一下文件個(gè)數(shù)便可以獲得 CPU 核數(shù)。

public static int getNumberOfCPUCores() {
 if (Build.VERSION.SDK_INT <= Build.VERSION_CODES.GINGERBREAD_MR1) {
 // Gingerbread doesn't support giving a single application access to both cores, but a
 // handful of devices (Atrix 4G and Droid X2 for example) were released with a dual-core
 // chipset and Gingerbread; that can let an app in the background run without impacting
 // the foreground application. But for our purposes, it makes them single core.
 return 1;
 }
 int cores;
 try {
 cores = new File("/sys/devices/system/cpu/").listFiles(CPU_FILTER).length;
 } catch (SecurityException e) {
 cores = DEVICEINFO_UNKNOWN;
 } catch (NullPointerException e) {
 cores = DEVICEINFO_UNKNOWN;
 }
 return cores;
}
private static final FileFilter CPU_FILTER = new FileFilter() {
 @Override
 public boolean accept(File pathname) {
 String path = pathname.getName();
 //regex is slow, so checking char by char.
 if (path.startsWith("cpu")) {
  for (int i = 3; i < path.length(); i++) {
  if (path.charAt(i) < '0' || path.charAt(i) > '9') {
   return false;
  }
  }
  return true;
 }
 return false;
 }
};

獲取時(shí)鐘頻率

獲取時(shí)鐘頻率需要讀取系統(tǒng)文件 - /sys/devices/system/cpu/cpu0/cpufreq/cpuinfo_max_freq 或者 /proc/cpuinfo。

我的 Android 模擬器中并沒(méi)有 cpuinfo_max_freq 文件,因此只能讀取 /proc/cpuinfo。

/proc/cpuinfo 包含了很多 cpu 數(shù)據(jù)。

processor   : 0
vendor_id   : GenuineIntel
cpu family  : 6
model       : 70
model name  : Intel(R) Core(TM) i7-4770HQ CPU @ 2.20GHz
stepping    : 1
cpu MHz     : 0.000
cache size  : 1024 KB
fdiv_bug    : no
hlt_bug     : no
f00f_bug    : no
coma_bug    : no
fpu     : yes
fpu_exception   : yes
cpuid level : 4
wp      : yes

代碼如下:

public static int getCPUMaxFreqKHz() {
 int maxFreq = DEVICEINFO_UNKNOWN;
 try {
 for (int i = 0; i < getNumberOfCPUCores(); i++) {
  String filename =
   "/sys/devices/system/cpu/cpu" + i + "/cpufreq/cpuinfo_max_freq";
  File cpuInfoMaxFreqFile = new File(filename);
  if (cpuInfoMaxFreqFile.exists()) {
  byte[] buffer = new byte[128];
  FileInputStream stream = new FileInputStream(cpuInfoMaxFreqFile);
  try {
   stream.read(buffer);
   int endIndex = 0;
   //Trim the first number out of the byte buffer.
   while (buffer[endIndex] >= '0' && buffer[endIndex] <= '9'
    && endIndex < buffer.length) endIndex++;
   String str = new String(buffer, 0, endIndex);
   Integer freqBound = Integer.parseInt(str);
   if (freqBound > maxFreq) maxFreq = freqBound;
  } catch (NumberFormatException e) {
   //Fall through and use /proc/cpuinfo.
  } finally {
   stream.close();
  }
  }
 }
 if (maxFreq == DEVICEINFO_UNKNOWN) {
  FileInputStream stream = new FileInputStream("/proc/cpuinfo");
  try {
  int freqBound = parseFileForValue("cpu MHz", stream);
  freqBound *= 1000; //MHz -> kHz
  if (freqBound > maxFreq) maxFreq = freqBound;
  } finally {
  stream.close();
  }
 }
 } catch (IOException e) {
 maxFreq = DEVICEINFO_UNKNOWN; //Fall through and return unknown.
 }
 return maxFreq;
}

獲取內(nèi)存大小

如果 SDK 版本大于等于 JELLY_BEAN ,可以通過(guò) ActivityManager 來(lái)獲取內(nèi)從大小。

ActivityManager.MemoryInfo memInfo = new ActivityManager.MemoryInfo();
ActivityManager am = (ActivityManager) c.getSystemService(Context.ACTIVITY_SERVICE);
am.getMemoryInfo(memInfo);

如果版本低于 JELLY_BEAN ,則只能讀取系統(tǒng)文件了。

FileInputStream stream = new FileInputStream("/proc/meminfo");
totalMem = parseFileForValue("MemTotal", stream);

完整代碼如下:

@TargetApi(Build.VERSION_CODES.JELLY_BEAN)
public static long getTotalMemory(Context c) {
 // memInfo.totalMem not supported in pre-Jelly Bean APIs.
 if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
 ActivityManager.MemoryInfo memInfo = new ActivityManager.MemoryInfo();
 ActivityManager am = (ActivityManager) c.getSystemService(Context.ACTIVITY_SERVICE);
 am.getMemoryInfo(memInfo);
 if (memInfo != null) {
  return memInfo.totalMem;
 } else {
  return DEVICEINFO_UNKNOWN;
 }
 } else {
 long totalMem = DEVICEINFO_UNKNOWN;
 try {
  FileInputStream stream = new FileInputStream("/proc/meminfo");
  try {
  totalMem = parseFileForValue("MemTotal", stream);
  totalMem *= 1024;
  } finally {
  stream.close();
  }
 } catch (IOException e) {
 }
 return totalMem;
 }
}

更多關(guān)于Android相關(guān)內(nèi)容感興趣的讀者可查看本站專(zhuān)題:《Android視圖View技巧總結(jié)》、《Android操作XML數(shù)據(jù)技巧總結(jié)》、《Android編程之a(chǎn)ctivity操作技巧總結(jié)》、《Android資源操作技巧匯總》、《Android文件操作技巧匯總》、《Android操作SQLite數(shù)據(jù)庫(kù)技巧總結(jié)》、《Android操作json格式數(shù)據(jù)技巧總結(jié)》、《Android數(shù)據(jù)庫(kù)操作技巧總結(jié)》、《Android編程開(kāi)發(fā)之SD卡操作方法匯總》、《Android開(kāi)發(fā)入門(mén)與進(jìn)階教程》及《Android控件用法總結(jié)

希望本文所述對(duì)大家Android程序設(shè)計(jì)有所幫助。

相關(guān)文章

  • ViewPager判斷是向左劃還是右劃的實(shí)例

    ViewPager判斷是向左劃還是右劃的實(shí)例

    下面小編就為大家?guī)?lái)一篇ViewPager判斷是向左劃還是右劃的實(shí)例。小編覺(jué)得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧
    2017-04-04
  • Android rom解包打包工具

    Android rom解包打包工具

    這篇文章主要介紹了Android rom解包打包工具的相關(guān)資料,對(duì)rom解包打包相關(guān)知識(shí)感興趣的朋友可以參考下
    2016-01-01
  • 實(shí)例詳解Android Webview攔截ajax請(qǐng)求

    實(shí)例詳解Android Webview攔截ajax請(qǐng)求

    本篇內(nèi)容主要給大家講解了Android Webview攔截ajax請(qǐng)求的詳細(xì)講解,需要的朋友一起來(lái)學(xué)習(xí)一下。
    2017-11-11
  • Android開(kāi)發(fā)實(shí)現(xiàn)Gallery畫(huà)廊效果的方法

    Android開(kāi)發(fā)實(shí)現(xiàn)Gallery畫(huà)廊效果的方法

    這篇文章主要介紹了Android開(kāi)發(fā)實(shí)現(xiàn)Gallery畫(huà)廊效果的方法,結(jié)合具體實(shí)例形式分析了Android使用Gallery實(shí)現(xiàn)畫(huà)廊功能的具體操作技巧與相關(guān)注意事項(xiàng),需要的朋友可以參考下
    2017-06-06
  • Android重寫(xiě)TextView實(shí)現(xiàn)文字整齊排版的方法(附demo源碼下載)

    Android重寫(xiě)TextView實(shí)現(xiàn)文字整齊排版的方法(附demo源碼下載)

    這篇文章主要介紹了Android重寫(xiě)TextView實(shí)現(xiàn)文字整齊排版的方法,結(jié)合實(shí)例形式分析了Android重寫(xiě)TextView實(shí)現(xiàn)文字整齊排版的相關(guān)技巧,并附帶demo源碼供讀者下載參考,需要的朋友可以參考下
    2016-02-02
  • Android組件ListView列表簡(jiǎn)單使用

    Android組件ListView列表簡(jiǎn)單使用

    這篇文章主要為大家詳細(xì)介紹了Android組件ListView列表的簡(jiǎn)單使用,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2017-02-02
  • SimpleCommand實(shí)現(xiàn)上傳文件或視頻功能(四)

    SimpleCommand實(shí)現(xiàn)上傳文件或視頻功能(四)

    這篇文章主要介紹了SimpleCommand實(shí)現(xiàn)上傳文件或視頻功能,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2017-10-10
  • Android中ViewPager獲取當(dāng)前顯示的Fragment

    Android中ViewPager獲取當(dāng)前顯示的Fragment

    這篇文章主要介紹了Android中ViewPager獲取當(dāng)前顯示的Fragment的兩種方法,一種是使用 getSupportFragmentManager().findFragmentByTag()方法,另一種是重寫(xiě)適配器的 setPrimaryItem()方法,有需要的朋友可以參考借鑒,下面來(lái)一起看看吧。
    2017-01-01
  • Android?Choreographer源碼詳細(xì)分析

    Android?Choreographer源碼詳細(xì)分析

    Choreographer的作用主要是配合Vsync,給上層App的渲染提供一個(gè)穩(wěn)定的Message處理的時(shí)機(jī),也就是Vsync到來(lái)的時(shí)候,系統(tǒng)通過(guò)對(duì)Vsync信號(hào)周期的調(diào)整,來(lái)控制每一幀繪制操作的時(shí)機(jī)
    2022-08-08
  • Android 中使用RadioGroup和Fragment實(shí)現(xiàn)底部導(dǎo)航欄的功能

    Android 中使用RadioGroup和Fragment實(shí)現(xiàn)底部導(dǎo)航欄的功能

    這篇文章主要介紹了Android 中使用RadioGroup+Fragment實(shí)現(xiàn)底部導(dǎo)航欄的功能,整體文章大概分為兩部分介紹,通過(guò)實(shí)例代碼給大家介紹的非常詳細(xì),需要的朋友可以參考下
    2021-06-06

最新評(píng)論

介休市| 饶阳县| 额敏县| 凤凰县| 色达县| 合阳县| 西乡县| 长阳| 永和县| 沁水县| 兴隆县| 嘉峪关市| 太仆寺旗| 房产| 祥云县| 怀柔区| 澳门| 永嘉县| 辽宁省| 安宁市| 莱阳市| 宜兰市| 孝感市| 垣曲县| 江北区| 靖江市| 图木舒克市| 牡丹江市| 甘肃省| 江阴市| 阜南县| 温宿县| 昌图县| 阳山县| 横山县| 牙克石市| 平乐县| 读书| 洛南县| 武邑县| 高唐县|