威尔TelephonyManger.getDeviceId像Galaxy Tab的平板电脑()返回的设备ID ...?
题
我想要得到的设备ID,将是每一个Android设备是唯一的。我目前正在开发一款平板设备。想要得到唯一的设备ID和存储相应的值...
所以,我想知道的平板设备是否会返回一个值,如果我使用TelephonyManager.getDeviceId()... ???或者有其他任何值,每个设备???
独特解决方案
这是不的一个重复的问题。事实证明,谷歌的CTS要求TelephonyManager的getPhoneType必须是没有和TelephonyManager的getDeviceId必须是在非移动电话设备的无效。
因此,要获得IMEI,请尽量使用:
String imei = SystemProperties.get("ro.gsm.imei")
不幸的是,SystemProperties是在Android OS,这意味着它是不公开的常规应用的非公共类。尝试寻找这个帖子求助访问它:哪里是android.os.SystemProperties
其他提示
<强> TelephonyManger.getDeviceId()强>返回唯一的设备ID,例如,IMEI对于GSM和MEID或ESN为CDMA电话。
final TelephonyManager mTelephony = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);
String myAndroidDeviceId = mTelephony.getDeviceId();
但是,我建议使用:
<强> Settings.Secure.ANDROID_ID 强>,返回的Android ID作为唯一的64位十六进制字符串。
String myAndroidDeviceId = Secure.getString(getApplicationContext().getContentResolver(), Secure.ANDROID_ID);
有时 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;
}
由于Android 8一切更改的内容。您应该使用Build.getSerial()
,来获得设备的序列号和添加权限READ_PHONE_STATE
。
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
serial = Build.getSerial(); // Requires permission READ_PHONE_STATE
} else {
serial = Build.SERIAL; // Will return 'unknown' for device >= Build.VERSION_CODES.O
}
和获得IMEI或MEID是这样的:
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
String imei = tm.getImei(); // Requires permission READ_PHONE_STATE
serial = imei == null ? tm.getMeid() : imei; // Requires permission READ_PHONE_STATE
} else {
serial = tm.getDeviceId(); // Requires permission READ_PHONE_STATE
}
不隶属于 StackOverflow