編輯:關於 Android 程式設計
開發行動應用時,取得裝置的硬體與系統資訊是很常見的需求,無論是用於診斷、統計還是顯示設定頁。本文整理在 Android 上讀取 RAM、ROM、SD 卡、電池電量、CPU、系統版本以及 MAC 位址與開機時間的實作方式。
Android 的總記憶體資訊存放在系統的 /proc/meminfo 檔案裡,讀取這個檔案即可取得各項數據:
public void getTotalMemory() {
String str1 = "/proc/meminfo";
String str2 = "";
try {
FileReader fr = new FileReader(str1);
BufferedReader localBufferedReader = new BufferedReader(fr, 8192);
while ((str2 = localBufferedReader.readLine()) != null) {
Log.i(TAG, "---" + str2);
}
} catch (IOException e) {
}
}
輸出的第一列 MemTotal 就是使用者可用的 RAM 總大小。若想取得當前剩餘記憶體,則可改用 ActivityManager:
public long getAvailMemory() {
ActivityManager am = (ActivityManager) mContext.getSystemService(Context.ACTIVITY_SERVICE);
ActivityManager.MemoryInfo mi = new ActivityManager.MemoryInfo();
am.getMemoryInfo(mi);
return mi.availMem;
}
public long[] getRomMemroy() {
long[] romInfo = new long[2];
// 內部儲存總容量
romInfo[0] = getTotalInternalMemorySize();
// 可用容量
File path = Environment.getDataDirectory();
StatFs stat = new StatFs(path.getPath());
long blockSize = stat.getBlockSize();
long availableBlocks = stat.getAvailableBlocks();
romInfo[1] = blockSize * availableBlocks;
getVersion();
return romInfo;
}
public long getTotalInternalMemorySize() {
File path = Environment.getDataDirectory();
StatFs stat = new StatFs(path.getPath());
long blockSize = stat.getBlockSize();
long totalBlocks = stat.getBlockCount();
return totalBlocks * blockSize;
}
要特別注意資料型別,否則兩值相乘後容易溢位。另外,內部儲存可用大小應以 getDataDirectory() 取得,而非網路上流傳的 getRootDirectory(),實測後者取到的數值並不正確。
public long[] getSDCardMemory() {
long[] sdCardInfo = new long[2];
String state = Environment.getExternalStorageState();
if (Environment.MEDIA_MOUNTED.equals(state)) {
File sdcardDir = Environment.getExternalStorageDirectory();
StatFs sf = new StatFs(sdcardDir.getPath());
long bSize = sf.getBlockSize();
long bCount = sf.getBlockCount();
long availBlocks = sf.getAvailableBlocks();
sdCardInfo[0] = bSize * bCount; // 總大小
sdCardInfo[1] = bSize * availBlocks; // 可用大小
}
return sdCardInfo;
}
同樣地,這裡也要注意型別,避免相乘溢位。
電量透過廣播接收器取得,level 加上百分比符號即為當前電量:
private BroadcastReceiver batteryReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
int level = intent.getIntExtra("level", 0);
// level 加 % 就是當前電量了
}
};
接著在 Activity 的 onCreate() 中註冊即可:
registerReceiver(batteryReceiver, new IntentFilter(Intent.ACTION_BATTERY_CHANGED));
public String[] getCpuInfo() {
String str1 = "/proc/cpuinfo";
String str2 = "";
String[] cpuInfo = {"", ""};
String[] arrayOfString;
try {
FileReader fr = new FileReader(str1);
BufferedReader localBufferedReader = new BufferedReader(fr, 8192);
str2 = localBufferedReader.readLine();
arrayOfString = str2.split("\\s+");
for (int i = 2; i < arrayOfString.length; i++) {
cpuInfo[0] = cpuInfo[0] + arrayOfString[i] + " ";
}
str2 = localBufferedReader.readLine();
arrayOfString = str2.split("\\s+");
cpuInfo[1] += arrayOfString[2];
localBufferedReader.close();
} catch (IOException e) {
}
return cpuInfo;
}
/proc/cpuinfo 的第一列是 CPU 型號,第二列是 CPU 頻率,讀檔即可解析出這些資料。
public String[] getVersion() {
String[] version = {"null", "null", "null", "null"};
String str1 = "/proc/version";
String str2;
String[] arrayOfString;
try {
FileReader localFileReader = new FileReader(str1);
BufferedReader localBufferedReader = new BufferedReader(localFileReader, 8192);
str2 = localBufferedReader.readLine();
arrayOfString = str2.split("\\s+");
version[0] = arrayOfString[2]; // KernelVersion
localBufferedReader.close();
} catch (IOException e) {
}
version[1] = Build.VERSION.RELEASE; // 韌體版本
version[2] = Build.MODEL; // 機型
version[3] = Build.DISPLAY; // 系統版本
return version;
}
版本資訊裡還包含機型等欄位,可依需求擴充。
public String[] getOtherInfo() {
String[] other = {"null", "null"};
WifiManager wifiManager = (WifiManager) mContext.getSystemService(Context.WIFI_SERVICE);
WifiInfo wifiInfo = wifiManager.getConnectionInfo();
if (wifiInfo.getMacAddress() != null) {
other[0] = wifiInfo.getMacAddress();
} else {
other[0] = "Fail";
}
other[1] = getTimes();
return other;
}
private String getTimes() {
long ut = SystemClock.elapsedRealtime() / 1000;
if (ut == 0) {
ut = 1;
}
int m = (int) ((ut / 60) % 60);
int h = (int) ((ut / 3600));
return h + " " + mContext.getString(R.string.info_times_hour)
+ m + " " + mContext.getString(R.string.info_times_minute);
}
以上就是取得 Android 系統各項資訊的常用做法,實際使用時別忘了在 AndroidManifest.xml 中補上對應的權限宣告。
MSM8909 + Android 5.1.1 鍵盤驅動剖析:從 I2C 到 Input 子系統
本文以 MSM8909 搭配 Android 5.1.1 的平台為例,梳理一個鍵盤驅動從硬體到框架層的完整鏈路。 一、整體概述 方案採用 SN7326 這顆具備智慧掃描能
LayoutInflater 的正确姿势:inflate 第三個參數為何不能亂傳
標題確實有點聳動,但 LayoutInflater 這個類在使用中存在的誤區,實在非常普遍——至少我看過的那些小項目源碼,幾乎都在用錯的方式調用它。日前讀到一篇講 Lay
用 SurfaceView 與属性動畫打造彈跳小球載入動畫
先看效果 這個動畫看起來簡單,真要做得自然卻需要幾個知識點配合: 1. 属性動畫(ValueAnimator / AnimatorSet) 2. Path 與 Canv
React Native for Android 環境搭建與第一個 App 完整實錄
一、為什麼值得投入 Facebook 先開源了 React Native for iOS,並在 2015 年 9 月 15 日補上了 Android 版本。雖然 Andr