编程语言
首页 > 编程语言> > java-tm.getDeviceId()已弃用?

java-tm.getDeviceId()已弃用?

作者:互联网

我正在获取IMEI和设备ID,所以这里我遇到了一个问题,不推荐使用getDeviceId().

TelephonyManager tm = (TelephonyManager) 
                 getSystemService(this.TELEPHONY_SERVICE);

imei = tm.getDeviceId();
device = Settings.Secure.getString(this.getContentResolver(),
           Settings.Secure.ANDROID_ID);

解决方法:

getDeviceId()

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

This method was deprecated in API level 26.

Use (@link getImei} which returns IMEI for GSM

or (@link getMeid} which returns MEID for CDMA.

有关更多信息,请阅读TelephonyManager

Try this to get IMEI

 @RequiresApi(api = Build.VERSION_CODES.O)
 TelephonyManager tm = (TelephonyManager)
            getSystemService(this.TELEPHONY_SERVICE);
    String imei = tm.getImei();

要么

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            String imei = telephonyMgr.getImei();
} else {
            String imei = telephonyMgr.getDeviceId();
}

Try this to get MEID

@RequiresApi(api = Build.VERSION_CODES.O)
TelephonyManager tm = (TelephonyManager)
            getSystemService(this.TELEPHONY_SERVICE);

    String meid=tm.getMeid();

要么

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            String meid=tm.getMeid();
} 

标签:android-8-0-oreo,telephonymanager,android,java
来源: https://codeday.me/bug/20190923/1814028.html