像IPhone一样的Android UDID?

时间:2010-07-12 05:50:04

标签: iphone android udid

Android有像IPhone这样的UDID吗?如果是的话,有没有办法以编程方式获得它?

由于 克里斯

4 个答案:

答案 0 :(得分:12)

来自docs

  

getDeviceId()

     

返回唯一的设备ID   例如,用于GSM的IMEI和MEID   用于CDMA手机。如果设备返回null   ID不可用。

答案 1 :(得分:8)

获取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,您无需设置任何权限。

答案 2 :(得分:1)

如果您在启动时将手机与Google帐户相关联,即在模拟器上无法使用,则只有在您注册Market时,设备ID才可用。这似乎已经在Android 2.2中发生了变化,其中也为仿真器生成了一个。我不相信它与IMEI,ICC或任何其他与手机相关的令牌有关,而是由Google网络服务生成的伪唯一令牌来识别您的手机。

答案 3 :(得分:1)

我实现了一个类来获取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());
}
}