22

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);
AskNilesh
  • 67,701
  • 16
  • 123
  • 163
ravi
  • 474
  • 1
  • 3
  • 11
  • Please read [Under what circumstances may I add “urgent” or other similar phrases to my question, in order to obtain faster answers?](//meta.stackoverflow.com/q/326569) - the summary is that this is not an ideal way to address volunteers, and is probably counterproductive to obtaining answers. Please refrain from adding this to your questions. – halfer Oct 14 '17 at 11:49

6 Answers6

24

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();
} 
Community
  • 1
  • 1
AskNilesh
  • 67,701
  • 16
  • 123
  • 163
8

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;
}
Bingerz
  • 1,027
  • 1
  • 11
  • 15
7

Edit: Since Android 10, you cannot request IMEI and MEID unless you have READ_PRIVILEGED_PHONE_STATE permission

Should not it be something like this?

            if (Build.VERSION.SDK_INT >= 26) {
                if (telMgr.getPhoneType() == TelephonyManager.PHONE_TYPE_CDMA) {
                    deviceId = telMgr.getMeid();
                } else if (telMgr.getPhoneType() == TelephonyManager.PHONE_TYPE_GSM) {
                    deviceId = telMgr.getImei();
                } else {
                    deviceId = ""; // default!!!
                }
            } else {
                deviceId = telMgr.getDeviceId();
            }
karim
  • 15,408
  • 7
  • 58
  • 96
4

copy and paste my this program and understood. we face issue here only in Permission (Run time and Check Permission Type );- Now i complete it and paste here a accurate program:

import android.*;
import android.Manifest;
import android.annotation.SuppressLint;
import android.content.Context;
import android.content.pm.PackageManager;
import android.os.Build;
import android.support.annotation.RequiresApi;
import android.support.v4.app.ActivityCompat;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.telephony.TelephonyManager;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import java.util.UUID;

public class Login extends AppCompatActivity {

    private Button loginBtn;
    private TextView textView;
    private String IMEINumber;




    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);
        loginBtn = (Button) findViewById(R.id.loginBtn);
        textView = (TextView) findViewById(R.id.textView);
        final int reqcode = 1;


        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M)
        {
            String[] per = {Manifest.permission.READ_PHONE_STATE};
            requestPermissions(per, reqcode);
            loginBtn.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    TelephonyManager tm = (TelephonyManager) getSystemService(TELEPHONY_SERVICE);
                    if (ActivityCompat.checkSelfPermission(Login.this, Manifest.permission.READ_PHONE_STATE) != PackageManager.PERMISSION_GRANTED) {
                        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
                            IMEINumber = tm.getImei();
                            textView.setText(IMEINumber);
                        }
                    } else {
                        IMEINumber = tm.getDeviceId();
                        textView.setText(IMEINumber);
                    }
                }
            });


        }
    }

}
Pradeep Sheoran
  • 493
  • 6
  • 15
  • 2
    Generally, answers are much more helpful if they include an explanation of what the code is intended to do, and why that solves the problem without introducing others. Thanks for improving the answer's reference value and making it more understandable! – Tim Diekmann Jun 04 '18 at 12:46
4

Kotlin Code for getting DeviceId ( IMEI ) with handling with permission & compatibility check for all android versions :

 val  telephonyManager = getSystemService(Context.TELEPHONY_SERVICE) as TelephonyManager
    if (ContextCompat.checkSelfPermission(this, Manifest.permission.READ_PHONE_STATE)
        == PackageManager.PERMISSION_GRANTED) {
        // Permission is  granted
        val imei : String? = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
             telephonyManager.imei
         } else { // older OS  versions
             telephonyManager.getDeviceId()
         }

        imei?.let {
            Log.i("Log", "DeviceId=$imei" )
        }

    } else {  // Permission is not granted

    }

Also add this permission to AndroidManifest.xml :

<uses-permission android:name="android.permission.READ_PHONE_STATE"/> <!-- IMEI-->
Hamed Jaliliani
  • 2,789
  • 24
  • 31
0

Since Android 10, you shouldn't request IMEI
https://developer.android.com/training/articles/user-data-ids#java

Workarround:

TelephonyManager telephonyManager = (TelephonyManager)context.getSystemService(Context.TELEPHONY_SERVICE);
if (null != telephonyManager) {
    if (telephonyManager.getDeviceId() != null){
        deviceUniqueIdentifier = telephonyManager.getDeviceId();
    }else{
        //Workarroud here
        deviceUniqueIdentifier = Settings.Secure.getString(context.getContentResolver(), Settings.Secure.ANDROID_ID);
    }
}
Lucio Pelinson
  • 430
  • 5
  • 7