Android UDID like IPhone?

前端 未结 4 996
猫巷女王i
猫巷女王i 2021-02-04 13:58

Does Android have a UDID like IPhone? If yes, is there a way I can get it programatically?

Thanks Chris

相关标签:
4条回答
  • 2021-02-04 14:37

    From the docs:

    getDeviceId()

    Returns the unique device ID, for example, the IMEI for GSM and the MEID for CDMA phones. Return null if device ID is not available.

    0 讨论(0)
  • 2021-02-04 14:56

    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.

    0 讨论(0)
  • 2021-02-04 15:02

    It's very easy to get the Android UDID - check out the following code:

    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:

    <uses-permission android:name="android.permission.READ_PHONE_STATE"/>
    

    For getting the Android ID you don't need to set any permission.

    0 讨论(0)
  • 2021-02-04 15:03

    I implemented a class to get IMEI / Wifi MAC address / deviceID, hope it useful for you ^^

    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());
    }
    }
    
    0 讨论(0)
提交回复
热议问题