Will TelephonyManger.getDeviceId() return device id for Tablets like Galaxy Tab…?

前端 未结 3 538
深忆病人
深忆病人 2020-12-05 11:37

I want to get the device id that will be unique for each Android device. I am presently developing for a Tablet device. Want to get unique device id and store the correspond

3条回答
  •  野趣味
    野趣味 (楼主)
    2020-12-05 11:45

    TelephonyManger.getDeviceId() Returns the unique device ID, for example, the IMEI for GSM and the MEID or ESN for CDMA phones.

    final TelephonyManager mTelephony = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);            
    String myAndroidDeviceId = mTelephony.getDeviceId(); 
    

    But i recommend to use:

    Settings.Secure.ANDROID_ID that returns the Android ID as an unique 64-bit hex string.

        String   myAndroidDeviceId = Secure.getString(getApplicationContext().getContentResolver(), Secure.ANDROID_ID); 
    

    Sometimes TelephonyManger.getDeviceId() will return null, so to assure an unique id you will use this method:

    public String getUniqueID(){    
        String myAndroidDeviceId = "";
        TelephonyManager mTelephony = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);
        if (mTelephony.getDeviceId() != null){
            myAndroidDeviceId = mTelephony.getDeviceId(); 
        }else{
             myAndroidDeviceId = Secure.getString(getApplicationContext().getContentResolver(), Secure.ANDROID_ID); 
        }
        return myAndroidDeviceId;
    }
    

提交回复
热议问题