官方博客
獲取mac地址
各種ID
獲取唯一性ID,各種ID的意義與限制
一.android_id
ANDROID_ID是Android系統第一次啟動時產生的一個64bit(16BYTES)數,如果設備被wipe還原后,該ID將被重置(變化)。
android.provider.Settings.Secure.getString(getContext().getContentResolver(),android.provider.Settings.Secure.ANDROID_ID);
二.序列號
android.os.Build.SERIAL
三.MAC地址
/**
* 6.0以下獲取mac地址
*/
public static String getMAC(Context context) {
WifiManager wifi = (WifiManager) context.getSystemService(context.WIFI_SERVICE);
WifiInfo info = wifi.getConnectionInfo();
String mac = info.getMacAddress();
return mac;
}
/**
* andorid 6.0以上獲取MAC地址
* @param context
* @return
*/
public static String getMacBeyondM(Context context) {
WifiManager wifi = (WifiManager) context.getSystemService(context.WIFI_SERVICE);
if(!wifi.isWifiEnabled()){
wifi.setWifiEnabled(true);
}
String macSerial = null;
String str = "";
try {
String wifiInterfaceName = SystemPropertiesProxy.get(context, "wifi.interface");
File file = new File("/sys/class/net/" + wifiInterfaceName + "/address");
boolean exist = file.exists();
Log.i("文件存在", exist + "");
InputStream inputStream = new FileInputStream(file);
InputStreamReader ir = new InputStreamReader(inputStream);
LineNumberReader input = new LineNumberReader(ir);
for (; null != str; ) {
str = input.readLine();
if (str != null) {
macSerial = str.trim();// 去空格
break;
}
}
} catch (IOException ex) {
// 賦予默認值
ex.printStackTrace();
}
Log.i("fileMac", macSerial + "");
return macSerial;
}
四.獲取唯一性ID
/**
* 獲取設備唯一ID
* @param context
* @return
*/
public static String getDeviceUniqID(Context context) {
android.telephony.TelephonyManager tm = (android.telephony.TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
String unique_id ;
unique_id = tm.getDeviceId();
if (TextUtils.isEmpty(unique_id)) {
unique_id=android.os.Build.SERIAL;
}
return unique_id;
}