4

As Android is serious about security and trying to make new android versions more secure, its becoming tough for developers to keep up-to date with new security features and find old methods alternatives to make their app compatible with old features.

This question is about IMEI in New Android 10! The old method was super easy to get IMEI number by using below code

  String deviceId = "";

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

In New Android 10 it is now restricted to get IMEI number. According to android documentation

apps must have the READ_PRIVILEGED_PHONE_STATE privileged permission in order to access the device's non-resettable identifiers, which include both IMEI and serial number.

The problem is that when we try to ask run time permissions with

  android.Manifest.permission.READ_PRIVILEGED_PHONE_STATE

My compiler does not recognize this permissions and i got error on this line but when i ask this permission in manifest file it does recognize this line but through warning that this permission is only for system apps.

I want to make my app compatible with Android 10 and want to get IMEI. How can i get IMEI number in Android 10 without becoming device owner or profile owner ?

Zaeem Sattar
  • 990
  • 2
  • 12
  • 30
  • 1
    you cannot access IMEI in android 10 unless you're developing a system-level app. You can use other unique identifiers though: https://developer.android.com/training/articles/user-data-ids – Nikos Hidalgo Sep 18 '19 at 13:08
  • 1
    Possible duplicate of [I am getting IMEI null in Android Q?](https://stackoverflow.com/questions/55173823/i-am-getting-imei-null-in-android-q) – Nikos Hidalgo Sep 18 '19 at 13:12

3 Answers3

3

Only device owner apps can read unique identifiers or your app must be a system app with READ_PRIVILEGED_PHONE_STATE. You can't ask this permission for a normal app.

greywolf82
  • 21,813
  • 18
  • 54
  • 108
3

Android 10 Restricted developer to Access IMEI number.

You can have a alternate solution by get Software ID. You can use software id as a unique id. Please find below code as i use in Application.

 public static String getDeviceId(Context context) { 

    String deviceId;

    if (android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {

deviceId = Settings.Secure.getString(context.getContentResolver(),Settings.Secure.ANDROID_ID);

}else {

final TelephonyManager mTelephony = (TelephonyManager)context.getSystemService(Context.TELEPHONY_SERVICE);



     if (mTelephony.getDeviceId() != null) {
            deviceId = mTelephony.getDeviceId();
        } else {
            deviceId = Settings.Secure.getString(
                    context.getContentResolver(),
                    Settings.Secure.ANDROID_ID);
        }
    }

return deviceId;

}
Satish
  • 527
  • 6
  • 13
  • Thank you it works and save my day – Priya Vasoya Oct 04 '21 at 11:20
  • this one line enough -> "deviceId = Settings.Secure.getString(context.getContentResolver(),Settings.Secure.ANDROID_ID);" You can't get READ_PRIVILEGED_PHONE_STATE permission for normal applications – Ranjithkumar Mar 22 '22 at 12:40
1

To get a unique identifier you can use FirebaseInstanceId.getInstance().getId(); or String uniqueID = UUID.randomUUID().toString();. Have a look here for best practices https://developer.android.com/training/articles/user-data-ids#java

Joe Malebe
  • 614
  • 5
  • 6