開發Android應用中,我們常常需要設備的唯一碼來確定客戶端。
Android 中的幾中方法,使用中常常不可靠
android 底層是 Linux,我們還是用Linux的方法來獲取:
1 cpu號:
文件在: /proc/cpuinfo
通過Adb shell 查看:
adb shell cat /proc/cpuinfo
2 mac 地址
文件路徑 /sys/class/net/wlan0/address
adb shell cat /sys/class/net/wlan0/address
xx:xx:xx:xx:xx:aa
這樣可以獲取兩者的序列號,
方法確定,剩下的就是寫代碼了
以Mac地址爲例:
String getMac() {String macSerial = null;
String str = "";
try {
Process pp = Runtime.getRuntime().exec(
"cat /sys/class/net/wlan0/address ");
InputStreamReader ir = new InputStreamReader(pp.getInputStream());
LineNumberReader input = new LineNumberReader(ir);
for (; null != str;) {
str = input.readLine();
if (str != null) {
macSerial = str.trim();// 去空格
break;
}
}
} catch (IOException ex) {
// 賦予默認值
ex.printStackTrace();
}
return macSerial;
}