0
@SuppressLint({"MissingPermission", "NewApi"})
public void getDeviceIMEI() {
    String deviceUniqueIdentifier = null;
    String deviceImei = null;
    if (null != telephonyManager) {
        deviceUniqueIdentifier = telephonyManager.getDeviceId();
        Log.d("Output_getDeviceIMEI", deviceUniqueIdentifier + "");
    }
//   

     if (null == deviceUniqueIdentifier || 0 == deviceUniqueIdentifier.length()) {
//            deviceUniqueIdentifier = Settings.Secure.getString(this.getContentResolver(), Settings.Secure.ANDROID_ID);
//            Log.d("Output_getDev_null", deviceUniqueIdentifier + "");
//        }

    if (null != telephonyManager) {
        if ( Build.VERSION.SDK_INT <= Build.VERSION_CODES.O_MR1 && Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
            deviceImei = telephonyManager.getImei();
            Log.d("Output_deviceImei_IMEI", deviceImei + "");
        }

    }
    if (null == deviceImei || 0 == deviceImei.length()) {
        deviceImei = Settings.Secure.getString(this.getContentResolver(), Settings.Secure.ANDROID_ID);
        Log.d("Output_deviceImei", deviceImei + "");
    }

}

Sagar
  • 23,903
  • 4
  • 62
  • 62
  • my problem is "telephonyManager.getDeviceId(); " this method which down't work after the marshmallow because when i write it, it ask for permission check for oreo which i have already given but even though I couldn't get IMEI number for android version 8.1. – Priyanka Yadav May 30 '18 at 07:08
  • " telephonyManager.getDeviceId();" my problem is this method which doesn't work after 6.0 & I want for 8.1 device version ie oreo – Priyanka Yadav May 30 '18 at 07:09
  • You are already using `deviceImei = telephonyManager.getImei();` still it doesn't work? – Sagar May 30 '18 at 07:11
  • please visit this [link](https://stackoverflow.com/questions/46744104/tm-getdeviceid-is-deprecated), it is relevant to your problem. – Ikram Hussain May 30 '18 at 07:15
  • Your if condition is wrong try this `if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O){deviceImei = telephonyManager.getImei();}else{deviceImei =telephonyMgr.getDeviceId();}` – Sunil Sunny May 30 '18 at 07:21
  • @ Ikram Hussain - I coldn't get the relevant data please share the exact url – Priyanka Yadav May 30 '18 at 07:57
  • @ Sagar - thq for ur reply but the log is below D/Output_getDev_null: e91010813f0dbf19 – Priyanka Yadav May 30 '18 at 07:58
  • @SUNIL - here is my code please inform me where I am wrong – Priyanka Yadav May 30 '18 at 08:02
  • @Override protected void onCreate(@Nullable Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_register); getDeviceIMEI(); } – Priyanka Yadav May 30 '18 at 08:02
  • @SUNIL - @SuppressLint("MissingPermission") public void getDeviceIMEI() { if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { deviceImei = telephonyManager.getImei(); Log.d("Output_getDev_null", deviceImei + ""); } else { deviceImei = telephonyManager.getDeviceId(); Log.d("Output_getDev", deviceImei + ""); } } – Priyanka Yadav May 30 '18 at 08:03
  • @SAGAR - @SuppressLint("MissingPermission") public void getDeviceIMEI() { if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { deviceImei = telephonyManager.getImei(); Log.d("Output_getDev_null", deviceImei + ""); } else { deviceImei = telephonyManager.getDeviceId(); Log.d("Output_getDev", deviceImei + ""); } } – Priyanka Yadav May 30 '18 at 08:04
  • @PriyankaYadav check the updated answer – Sagar May 30 '18 at 08:09
  • thq very much Sagar for your kind help – Priyanka Yadav May 30 '18 at 08:51
  • @Sagar - please check this – Priyanka Yadav May 30 '18 at 11:56
  • https://stackoverflow.com/questions/50603883/in-google-play-console-panel-apk-is-in-alpha-version-not-yet-public – Priyanka Yadav May 30 '18 at 11:56
  • @sagar hey please see my query on maps Required ur guidance – Priyanka Yadav May 31 '18 at 08:22
  • @sagar Yes I have – Priyanka Yadav May 31 '18 at 10:40
  • @Sagar - Send Email in background without user interface in Android programmatically plx check and reskm – Priyanka Yadav Jul 09 '18 at 11:03

1 Answers1

1

Starting from android 8 you need to use getImei(int slotIndex) to retrieve the IMEI of the device.

You also need to add <uses-permission android:name="android.permission.READ_PHONE_STATE"/> permission in your AndroidManifest.xml

Remember to implement permission model to request permission at runtime as follows:

ActivityCompat.requestPermissions(MainActivity.this,
                    new String[]{Manifest.permission.READ_PHONE_STATE},
                    1);
@Override
public void onRequestPermissionsResult(int requestCode,
                                       String permissions[], int[] grantResults) {
    switch (requestCode) {
        case 1: {

          // If request is cancelled, the result arrays are empty.
          if (grantResults.length > 0
                    && grantResults[0] == PackageManager.PERMISSION_GRANTED) {

               //continue using `getImei()` or `getDeviceId()`  
            } else {

               //Use device Id or use fallback case
            }
            return;
        }   
    }
}
Sagar
  • 23,903
  • 4
  • 62
  • 62