I have tried using 2 methods for retrieving my phone number but both of them don't work. I used:
- TelephonyManager
- SubscriptionManager
I do get Network name, Country iso, and IMEI but whenever I try to return Number it returns nothing.
I have also added all the required permissions for these! My manifest looks like:
<uses-permission android:name="android.permission.READ_PHONE_STATE" />
<uses-permission android:name="android.permission.READ_PHONE_NUMBERS" />
<uses-permission android:name="android.permission.GET_ACCOUNTS" />
Code using TelephonyManager:
TelephonyManager phoneMgr = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);
phoneMgr.getLine1Number()
Code using SubscriptionManager:
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.e("TAG", "number " + info.getNumber());
Log.e("TAG", "network name : " + info.getCarrierName());
Log.e("TAG", "country iso " + info.getCountryIso());
}
}
In both attempts I get nothing!
Is there any other way to get phone number or I'm doing something wrong?
Nowadays the
TelephonyManager
does not help us. Play Services API without permission is good solution for this.This dependency is useful for this
Now inside your Activity.java:
After this do request for Phone Number:
Now you need to handle response in your
onActivityResult
like this: