Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

tm.getDeviceId() is deprecated?

Tags:

I'm getting the IMEI and device Id's, so here I am getting a problem getDeviceId() is deprecated.

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

imei = tm.getDeviceId();
device = Settings.Secure.getString(this.getContentResolver(),
           Settings.Secure.ANDROID_ID);
like image 507
ravi Avatar asked Oct 14 '17 11:10

ravi


2 Answers

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.

for more information read TelephonyManager

Try this to get 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();
} 
like image 108
AskNilesh Avatar answered Sep 19 '22 06:09

AskNilesh


This is my solution:

@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;
}
like image 31
Bingerz Avatar answered Sep 21 '22 06:09

Bingerz