-1

In my Android application, I need to display phone numbers of the Sim cards which are available in the device.

I tried with the below codes

 private String getPhone() {
    TelephonyManager phoneMgr = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);
    if (ActivityCompat.checkSelfPermission(activity, wantPermission) != PackageManager.PERMISSION_GRANTED) {
        return "";
    }
    return phoneMgr.getLine1Number();
}

and

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
List<SubscriptionInfo> subscription = SubscriptionManager.from(getApplicationContext()).getActiveSubscriptionInfoList();
for (int i = 0; i < subscription.size(); i++) {
    SubscriptionInfo info = subscription.get(i);
    Log.d(TAG, "number " + info.getNumber());
    Log.d(TAG, "network name : " + info.getCarrierName());
    Log.d(TAG, "country iso " + info.getCountryIso());
}

}

But both are returning null or empty string only.

I have added the manifest permission also.

<uses-permission android:name="android.permission.READ_PHONE_STATE"/> 

Could you please help me.

Thanks.

surendra
  • 2,217
  • 7
  • 34
  • 42

1 Answers1

0

some phone companies not give this permission to get number from sim-card because i was same problem in past projects.

(in the past project i want to send OTP on simcard number user can't type his number. it was for security purpose.) so some of phone gives you output correct and remains give you output as NULL