Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲

獲取 Android 手機 CPU 類型:ARM、ARMV7 與 NEON 判斷

編輯:關於 Android 程式設計

在 Android 開發中,特別是涉及 Native 庫或影音解碼時,判斷手機 CPU 的架構與指令集至關重要。本文介紹如何透過命令列與程式碼兩種方式獲取 CPU 資訊。

一、查看手機 CPU 資訊

透過 adb 進入設備的 proc 目錄即可讀取 CPU 資訊:

adb shell → cd /proc → cat cpuinfo

二、判斷指令集類型

我們需要判斷 CPU 屬於 ARM 架構、V6 還是 V7 架構,以及是否支援 NEON 指令集。下面的方法會回傳一個包含三個參數的陣列,分別標識架構、版本與 NEON 支援情況:

public static Object[] getCpuArchitecture() {

if ((Integer) mArmArchitecture[1] != -1) {

return mArmArchitecture;

}

try {

InputStream is = new FileInputStream("/proc/cpuinfo");

InputStreamReader ir = new InputStreamReader(is);

BufferedReader br = new BufferedReader(ir);

try {

String nameProcessor = "Processor";

String nameFeatures = "Features";

String nameModel = "model name";

String nameCpuFamily = "cpu family";

while (true) {

String line = br.readLine();

if (line == null) break;

String[] pair = line.split(":");

if (pair.length != 2) continue;

String key = pair[0].trim();

String val = pair[1].trim();

if (key.compareTo(nameProcessor) == 0) {

String n = "";

for (int i = val.indexOf("ARMv") + 4; i < val.length(); i++) {

String temp = val.charAt(i) + "";

if (temp.matches("\\d")) { n += temp; } else { break; }

}

mArmArchitecture[0] = "ARM";

mArmArchitecture[1] = Integer.parseInt(n);

continue;

}

if (key.compareToIgnoreCase(nameFeatures) == 0) {

if (val.contains("neon")) { mArmArchitecture[2] = "neon"; }

continue;

}

if (key.compareToIgnoreCase(nameModel) == 0) {

if (val.contains("Intel")) {

mArmArchitecture[0] = "INTEL";

mArmArchitecture[2] = "atom";

}

continue;

}

if (key.compareToIgnoreCase(nameCpuFamily) == 0) {

mArmArchitecture[1] = Integer.parseInt(val);

continue;

}

}

} finally {

br.close(); ir.close(); is.close();

}

} catch (Exception e) {

e.printStackTrace();

}

return mArmArchitecture;

}

三、應用場景:FFmpeg 解碼庫的向下相容

在載入 FFmpeg 這類 Native 庫時,向下相容是常見問題。例如當前 CPU 為 V7 且支援 NEON,但若缺少對應的解碼庫,就必須逐級降級尋找:優先載入 V7 NEON 版本,其次 V7,最後才是 V6。下面的 getFFmpegLibName 方法展示了這種智慧降級策略:

public static String getFFmpegLibName(Context context) {

if (LIB_FFMPEG_NAME != null) return LIB_FFMPEG_NAME;

Object[] arch = getCpuArchitecture();

String libDir = getNativeLibraryDir(context);

String libSysDir = "/system/lib";

if ("ARM".equals(arch[0])) {

String ffmpeg = String.format("ffmpeg-%d%s", (Integer) arch[1], (String) arch[2]);

if (isFileExist(libDir + "/lib" + ffmpeg + ".so") || isFileExist(libSysDir + "/lib" + ffmpeg + ".so")) {

return ffmpeg;

}

boolean isV7NeonCpu = "neon".equals(arch[2]);

boolean isV7 = ((Integer) arch[1]) == 7 && "".equals(arch[2]);

boolean isV6 = ((Integer) arch[1]) == 6;

if (isV7NeonCpu) {

if (存在 7neon 庫) return "ffmpeg-7neon";

else if (存在 7 庫) return "ffmpeg-7";

else if (存在 6 庫) return "ffmpeg-6";

} else if (isV7) {

if (存在 7 庫) return "ffmpeg-7";

else if (存在 6 庫) return "ffmpeg-6";

} else if (isV6) {

if (存在 6 庫) return "ffmpeg-6";

}

} else if ("INTEL".equals(arch[0])) {

if (存在 x86atom 庫) return "ffmpeg-x86atom";

}

LIB_FFMPEG_NAME = null;

return null;

}

透過這種設計,應用在不同的硬體環境下都能找到最適配的解碼庫,大幅提升了相容性與穩定性。

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © Android教程網 All Rights Reserved