android设备获取设备唯一号


TelephonyManager mTelephonyMgr = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);
imei = mTelephonyMgr.getDeviceId();
if (isNull(imei)) {
	// 获取MAC
	imei = getMac();
	// 获取设备号, 恢复出厂设置可能会变
	// imei = Secure
	// .getString(getContentResolver(), Secure.ANDROID_ID);
}

System.out.println("imei:" + imei);

// 用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
// 此方法是linux下面获取mac的方法,保证在无wifi情况下也能获取到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) {
	}
	return macSerial;
}

猜你喜欢

转载自zheyiw.iteye.com/blog/2083246
今日推荐