0

TelephonyManager getSubscriberId() and getSimSerialNumber() return null in android Q!

I'm coding in java with android studio IDE!

TelephonyManager telephonyManager = (TelephonyManager) getSystemService(TELEPHONY_SERVICE);
telephonyManager.getSimSerialNumber(); //it is null
Ashish
  • 6,791
  • 3
  • 26
  • 48
Mahdi Bagheri
  • 23
  • 1
  • 9
  • Have you used permission in manifest> – Abhinav Gupta Jul 24 '19 at 10:04
  • Please read the Reference: https://developer.android.com/reference/android/telephony/TelephonyManager.html#getSimSerialNumber() – emandt Jul 24 '19 at 10:05
  • @Abhinav__Gupta I have added READ_PHONE_STATE permission and it is granted by the user. – Mahdi Bagheri Jul 24 '19 at 10:15
  • 1
    Possible duplicate of [Android: TelephonyManager.getSimSerialNumber() returns null](https://stackoverflow.com/questions/5552249/android-telephonymanager-getsimserialnumber-returns-null) – Masoud Keshavarz Jul 24 '19 at 10:25
  • @emandt I have read that but unfortunately I don't understand how to implement it. – Mahdi Bagheri Jul 24 '19 at 10:27
  • @MasoudKeshavarz Thx, This returns null only in android Q that's the problem! – Mahdi Bagheri Jul 24 '19 at 10:30
  • 1
    @MasoudKeshavarz it's all written in some guide/Reference like this: https://developer.android.com/preview/privacy/data-identifiers The Permission you need is PRIVILEGED and cannot be used by normal Apps: "Starting in Android Q, 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. - Caution: Third-party apps installed from the Google Play Store cannot declare privileged permissions." – emandt Jul 24 '19 at 10:40
  • @emandt So what's the solution? – Mahdi Bagheri Jul 24 '19 at 12:45
  • @MahdiBagheri (1) you can use Root Permissions to bypass this limit or (2) you can sign the final APK using the System/OS Certificate (but that specific APK could be executed only on that specific OS) – emandt Jul 24 '19 at 13:59

3 Answers3

1

If your apk's targetsdk <30, you can get ICCID by the following method without any permission even in android 11.

    Uri uri = Uri.parse("content://telephony/siminfo");
Cursor cursor = null;
ContentResolver contentResolver = getApplicationContext().getContentResolver();
cursor = contentResolver.query(uri,
        new String[]{"_id", "sim_id", "imsi","icc_id","number","display_name"}, "0=0",
        new String[]{}, null);
if (null != cursor) {
    while (cursor.moveToNext()) {
        String icc_id = cursor.getString(cursor.getColumnIndex("icc_id"));
        String imsi_id = cursor.getString(cursor.getColumnIndex("imsi"));
        String phone_num = cursor.getString(cursor.getColumnIndex("number"));
        String display_name = cursor.getString(cursor.getColumnIndex("display_name"));
        int sim_id = cursor.getInt(cursor.getColumnIndex("sim_id"));
        int _id = cursor.getInt(cursor.getColumnIndex("_id"));
        Log.d("Q_M", "icc_id-->" + icc_id);
        Log.d("Q_M", "imsi_id-->" + imsi_id);
        Log.d("Q_M", "phone_num-->" + phone_num);
        Log.d("Q_M", "sim_id-->" + sim_id);
        Log.d("Q_M", "display_name-->" + display_name);
    }
}
James Chen
  • 36
  • 2
0

public String getSimSerialNumber () Returns the serial number of the SIM, if applicable. Return null if it is unavailable.

Requires Permission: READ_PRIVILEGED_PHONE_STATE, for the calling app to be the device or profile owner and have the READ_PHONE_STATE permission.

See the following link: getSerialNumber()

M Karimi
  • 1,991
  • 1
  • 17
  • 34
0

Also if are your app is can be selectable as default SMS app you can get this data without need any permissions. For set your app as the default SMS app please look that page