[英]Android UDID like IPhone?
Android有像IPhone這樣的UDID嗎? 如果是的話,有沒有辦法以編程方式獲得它?
謝謝克里斯
來自文檔 :
getDeviceId()
返回唯一的設備ID,例如,GSM的IMEI和CDMA電話的MEID。 如果設備ID不可用,則返回null。
獲取Android UDID非常容易 - 請查看以下代碼:
public class DemoActivityActivity extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
TelephonyManager tm = (TelephonyManager) this.getSystemService(Context.TELEPHONY_SERVICE);
Log.d(">>>>", "Android ID: " + Secure.getString(getContentResolver(), Secure.ANDROID_ID));
Log.d(">>>>", "Device ID : " + tm.getDeviceId());
}
要獲取設備ID,您必須在AndroidManifest.xml中設置以下權限:
<uses-permission android:name="android.permission.READ_PHONE_STATE"/>
要獲取Android ID,您無需設置任何權限。
我實現了一個類來獲取IMEI / Wifi MAC地址/ deviceID,希望它對你有用^^
public class DeviceInfo {
protected static String imeiNumber;
protected static String wifiMacAddress;
protected static String deviceID;
// This method must be called before other method
public static void init(Context context) throws Exception {
imeiNumber = getImei(context);
wifiMacAddress = getWifiMacAddress(context);
deviceID = getDeviceId(context);
}
public static String getDeviceInfo() {
return deviceID;
}
public static String getImei() {
return imeiNumber;
}
public static String getWifiMacAddress() {
return wifiMacAddress;
}
public static String getModel() {
return Build.MODEL;
}
public static String getOsVersion() {
return Build.VERSION.RELEASE;
}
protected static String getDeviceId(Context context) throws Exception {
String imei = getImei(context);
if (imei != null) return imei;
String tid = getWifiMacAddress(context);
return tid;
}
protected static String getWifiMacAddress(Context context) throws Exception {
WifiManager manager = (WifiManager) context
.getSystemService(Context.WIFI_SERVICE);
WifiInfo wifiInfo = manager.getConnectionInfo();
if (wifiInfo == null || wifiInfo.getMacAddress() == null)
return md5(UUID.randomUUID().toString());
else return wifiInfo.getMacAddress().replace(":", "").replace(".", "");
}
protected static String getImei(Context context) {
TelephonyManager m = (TelephonyManager) context
.getSystemService(Context.TELEPHONY_SERVICE);
String imei = m != null ? m.getDeviceId() : null;
return imei;
}
protected static String md5(String s) throws Exception {
MessageDigest md = MessageDigest.getInstance("MD5");
md.update(s.getBytes());
byte digest[] = md.digest();
StringBuffer result = new StringBuffer();
for (int i = 0; i < digest.length; i++) {
result.append(Integer.toHexString(0xFF & digest[i]));
}
return (result.toString());
}
}
如果您在啟動時將手機與Google帳戶相關聯,即在模擬器上無法使用,則只有在您注冊Market時,設備ID才可用。 這似乎已經在Android 2.2中發生了變化,其中也為仿真器生成了一個。 我不認為它與IMEI,ICC或任何其他與手機相關的令牌有關,而是由Google網絡服務生成的偽唯一令牌來識別您的手機。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.