Android UDID 和 iPhone 一样吗?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3226135/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Android UDID like IPhone?
提问by Chris
Does Android have a UDID like IPhone? If yes, is there a way I can get it programatically?
Android 有像 iPhone 一样的 UDID 吗?如果是,有没有办法以编程方式获得它?
Thanks Chris
谢谢克里斯
回答by Macarse
回答by Sachin Shelke
It's very easy to get the Android UDID - check out the following code:
获取 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());
}
For getting the Device ID you have to set following permission in AndroidManifest.xml:
要获取设备 ID,您必须在 AndroidManifest.xml 中设置以下权限:
<uses-permission android:name="android.permission.READ_PHONE_STATE"/>
For getting the Android ID you don't need to set any permission.
要获取 Android ID,您无需设置任何权限。
回答by Hoàng To?n
I implemented a class to get IMEI / Wifi MAC address / deviceID, hope it useful for you ^^
我实现了一个获取IMEI/Wifi MAC地址/设备ID的类,希望对你有用^^
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());
}
}
回答by Fazal Majid
The Device ID used to only be available if you had signed up for Market by associating your phone with your Google account when you start, i.e. not available on the emulator. This seems to have changed with Android 2.2, where one is generated for the emulator as well. I don't believe it is associated with IMEI, ICC or any other phone-related token, but is rather a pseudo-unique token generated by Google web services to identify your phone.
设备 ID 过去仅当您在启动时通过将手机与 Google 帐户关联来注册 Market 时才可用,即在模拟器上不可用。这似乎在 Android 2.2 中发生了变化,其中也为模拟器生成了一个。我不认为它与 IMEI、ICC 或任何其他与手机相关的令牌相关联,而是由 Google 网络服务生成的伪唯一令牌,用于识别您的手机。