Tm.getDeviceId () is deprecated?

I get IMEI and device IDs, so here the problem getDeviceId() deprecated.

 TelephonyManager tm = (TelephonyManager) getSystemService(this.TELEPHONY_SERVICE); imei = tm.getDeviceId(); device = Settings.Secure.getString(this.getContentResolver(), Settings.Secure.ANDROID_ID); 
+5
source share
2 answers

getDeviceId()

Returns the unique identifier of the device to subscribe, for example, IMEI for GSM and MEID for CDMA phones. Return null if the device identifier is unavailable.

This method was deprecated at API level 26.

Use (@link getImei} , which returns IMEI for GSM

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

for more information read TelephonyManager

Try to do IMEI

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

OR

 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(); 

OR

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

This is my decision:

 @SuppressWarnings("deprecation") private String getIMEINumber() { String IMEINumber = ""; if (ActivityCompat.checkSelfPermission(this, android.Manifest.permission.READ_PHONE_STATE) == PackageManager.PERMISSION_GRANTED) { TelephonyManager telephonyMgr = (TelephonyManager) getSystemService(TELEPHONY_SERVICE); if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { IMEINumber = telephonyMgr.getImei(); } else { IMEINumber = telephonyMgr.getDeviceId(); } } return IMEINumber; } 
+1
source

Source: https://habr.com/ru/post/1272621/


All Articles