1

I have tried different links but none of them works:

  1. TelephonyManager returns null for IMEI number: what can cause this?

  2. How to get device's IMEI/ESN number with code programming But in android > 6

and many more links but none of them working.

This is my Utils Class:

public static String getIMEINumber(Context context){
  TelephonyManager telephonyManager = (TelephonyManager)context.getSystemService(Context.TELEPHONY_SERVICE);
  return telephonyManager.getDeviceId();
}

Calling below code from my Activity:

String deviceIMEI = Utils.getIMEINumber(LoginActivity.this);

But this is giving me null sometimes (when we install App for the first time particularly).Can you help me on this?

Thank you very much for your time and assistance in this matter.

ישו אוהב אותך
  • 28,609
  • 11
  • 78
  • 96
Naveen
  • 481
  • 2
  • 6
  • 23

1 Answers1

0

See sample:

private int REQUEST_PERMISSION_PHONE_STATE = 1;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        showPhoneStatePermission();
    }


    private void showPhoneStatePermission() {
        int permissionCheck = ContextCompat.checkSelfPermission(
                this, Manifest.permission.READ_PHONE_STATE);
        if (permissionCheck != PackageManager.PERMISSION_GRANTED) {
            requestPermission(Manifest.permission.READ_PHONE_STATE, REQUEST_PERMISSION_PHONE_STATE);
        } else {
            getIMEI();
        }
    }

    private void getIMEI() {
        String deviceId = IMEIUtil.getDeviceId(this);
        toastMessage(deviceId);
    }

    private void requestPermission(String permissionName, int permissionRequestCode) {
        ActivityCompat.requestPermissions(this,
                new String[]{permissionName}, permissionRequestCode);
    }

    private void toastMessage(String msg) {
        Toast.makeText(this, msg, Toast.LENGTH_SHORT).show();
    }


    @Override
    public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);

        if (requestCode == REQUEST_PERMISSION_PHONE_STATE) {
            showPhoneStatePermission();
        }
    }

And class IMEIUtil:

public static String getDeviceId(Context context) {
        TelephonyManager telephonyManager = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
        String deviceId = telephonyManager.getDeviceId().trim();
        if (deviceId == null) {
            String androidId = Settings.Secure.getString(context.getContentResolver(),
                    Settings.Secure.ANDROID_ID);
            deviceId = android.os.Build.SERIAL + "#" + androidId;
        }
        return deviceId.trim();
    }

Require add permission <uses-permission android:name="android.permission.READ_PHONE_STATE" /> before tag <application> on AndroidManifest. You can see full code: https://github.com/HoangDang/GetIMEI/tree/master/GetIMEI. I hope it can help you!

Dungnbhut
  • 176
  • 5