編輯:關於 Android 程式設計
在 Android 開發中,特別是涉及 Native 庫或影音解碼時,判斷手機 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 這類 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;
}
透過這種設計,應用在不同的硬體環境下都能找到最適配的解碼庫,大幅提升了相容性與穩定性。
Android 中 ListView setOnItemClickListener 點擊無效的原因分析
前言 最近做專案的過程中,使用 ListView 時遇到了設定 item 監聽事件卻不觸發 onItemClick 回呼的問題。我的情況是 item 裡有一個 Butto
Android 菜單半透明效果實作:Shape + GridView + PopupWindow 三步驟
用過「天天動聽」的朋友,大概都對它那層半透明的彈出菜單有印象:浮在介面上方,隱隱透出背後的內容,質感相當細膩。本文就以這種效果為目標,把整個實作過程拆解開來,並附上可以直
Android 自定義組合控件(三):用自定義標題欄提升布局復用
前言:前兩篇我們介紹了自定義控件的基礎原理與自定義屬性。今天要重點說明的是,如何透過自定義組合控件來提高版面配置的复用性,从而降低开发與維護成本。使用自定義組合控件有什麼
Android Studio 效率插件推薦:從接口調試到模擬器加速
工欲善其事,必先利其器。Android Studio 本身已經足夠強大,但裝上幾個合適的插件之後,日常開發的效率會有明顯提升。以下按使用頻次由高到低整理,前面幾個是幾乎每