TelephonyManger.getDeviceId()会返回像Galaxy Tab这样的平板电脑的设备ID吗?

Rah*_*rma 16 android tablet-pc

我想获得每个Android设备唯一的设备ID.我目前正在开发平板电脑设备.想获得唯一的设备ID并存储相应的值...

所以,我想知道如果我使用TelephonyManager.getDeviceId()......,平板电脑设备是否会返回一个值?或者是否有任何其他值对每个设备唯一?

Jor*_*sys 59

TelephonyManger.getDeviceId()返回唯一的设备ID,例如,GSM的IMEI和CDMA电话的MEID或ESN.

final TelephonyManager mTelephony = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);            
String myAndroidDeviceId = mTelephony.getDeviceId(); 
Run Code Online (Sandbox Code Playgroud)

但我建议使用:

Settings.Secure.ANDROID_ID,它将Android ID作为唯一的64位十六进制字符串返回.

    String   myAndroidDeviceId = Secure.getString(getApplicationContext().getContentResolver(), Secure.ANDROID_ID); 
Run Code Online (Sandbox Code Playgroud)

有时TelephonyManger.getDeviceId()将返回null,因此为了确保您将使用此方法的唯一ID:

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;
}
Run Code Online (Sandbox Code Playgroud)

  • 那么为什么不一直只使用Secure.ANDROID_ID呢? (6认同)
  • 该解决方案存在一个问题:当用户切换到飞行模式时,电话信息消失,该方法将返回"Secure.ANDROID_ID"值.因此`getUniqueID()`方法可能会为同一设备的不同状态返回不同的值. (4认同)
  • 用户设置设备时会生成ANDROID_ID.如果再次重置设备,则ID可能会更改. (3认同)

Mat*_*ley 9

不是一个重复的问题.事实证明,Google的CTS要求TelephonyManager的getPhoneType需要为none,而对于非电话设备,TelephonyManager的getDeviceId需要为null.

所以要获得IMEI,请尝试使用:

String imei = SystemProperties.get("ro.gsm.imei")
Run Code Online (Sandbox Code Playgroud)

不幸的是,SystemProperties是Android操作系统中的非公共类,这意味着常规应用程序无法公开使用它.尝试查看这篇文章以获取访问权限:android.os.SystemProperties在哪里