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

发布于 2024-09-25 05:18:25 字数 164 浏览 5 评论 0原文

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

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

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 corresponding values...

So, i want to know whether Tablet devices will return a value if i use TelephonyManager.getDeviceId()...???Or is there any other value that is unique for each device???

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

蓝天 2024-10-02 05:18:25

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

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;
}

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;
}
守不住的情 2024-10-02 05:18:25

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

因此,要获取 IMEI,请尝试使用:

String imei = SystemProperties.get("ro.gsm.imei")

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

This is not a duplicate question. As it turns out, Google's CTS require that getPhoneType of TelephonyManager needs to be none and getDeviceId of TelephonyManager needs to be null for non-phone devices.

So to get IMEI, please try to use:

String imei = SystemProperties.get("ro.gsm.imei")

Unfortunately, SystemProperties is a non-public class in the Android OS, which means it isn't publicly available to regular applications. Try looking at this post for help accessing it: Where is android.os.SystemProperties

寻找一个思念的角度 2024-10-02 05:18:25

自 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
}

Since Android 8 everything's changed. You should use Build.getSerial(), to get the serial number of the device and add the permission 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
}

And get the IMEI or MEID this way:

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
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文