I have tried using 2 methods for retrieving my phone number but both of them don't work. I used:
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?
On Android the most common path to finding your number is: Settings > About phone/device > Status/phone identity > Network. This slightly differs on Apple devices, where you can follow the path of Settings > Phone > My Number.
Nowadays the TelephonyManager
does not help us. Play Services API without permission is good solution for this.
This dependency is useful for this
implementation 'com.google.android.gms:play-services-auth:16.0.1'
Now inside your Activity.java:
GoogleApiClient mGoogleApiClient = new GoogleApiClient.Builder(this)
.addConnectionCallbacks(this)
.addOnConnectionFailedListener(this)
.addApi(Auth.CREDENTIALS_API)
.build();
if (mGoogleApiClient != null) {
mGoogleApiClient.connect();
}
After this do request for Phone Number:
HintRequest hintRequest = new HintRequest.Builder()
.setPhoneNumberIdentifierSupported(true)
.build();
PendingIntent intent = Auth.CredentialsApi.getHintPickerIntent(mGoogleApiClient, hintRequest);
try {
startIntentSenderForResult(intent.getIntentSender(), 1008, null, 0, 0, 0, null);
} catch (IntentSender.SendIntentException e) {
Log.e("", "Could not start hint picker Intent", e);
}
Now you need to handle response in your onActivityResult
like this:
@Override
public void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
super.onActivityResult(requestCode, resultCode, data);
switch (requestCode) {
case 1008:
if (resultCode == RESULT_OK) {
Credential cred = data.getParcelableExtra(Credential.EXTRA_KEY);
// cred.getId====: ====+919*******
Log.e("cred.getId", cred.getId());
userMob = cred.getId();
} else {
// Sim Card not found!
Log.e("cred.getId", "1008 else");
return;
}
break;
}
}
I found @bhoomika's answer useful but now using GoogleApiClient is deprecated. So you can use CredentialsClient instead.
Below is the method I used to trigger the phone number hint dialog (this method is in a helper class).
public void requestPhoneNumberHint(Activity currentActivity) {
HintRequest hintRequest = new HintRequest.Builder()
.setPhoneNumberIdentifierSupported(true)
.build();
CredentialsClient credentialsClient = Credentials.getClient(currentActivity);
PendingIntent intent = credentialsClient.getHintPickerIntent(hintRequest);
try {
uiListener.getCurrentActivity().startIntentSenderForResult(intent.getIntentSender(),
RESOLVE_PHONE_NUMBER_HINT, null, 0, 0, 0);
} catch (IntentSender.SendIntentException e) {
e.printStackTrace();
}
}
Below is my handling for the corresponding onActivityResult (my Activity code is in Kotlin)
override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
if(requestCode == RESOLVE_PHONE_NUMBER_HINT){
if (resultCode == RESULT_OK) {
var credential : Credential? = data?.getParcelableExtra(Credential.EXTRA_KEY)
credential?.apply {
processPhoneNumber(id)
}
}
}
As mentioned in the below documentation, the result codes can be used to identify if there were no hints that were displayed or if the user did not chose any of the options.
public static final int ACTIVITY_RESULT_NO_HINTS_AVAILABLE Activity result code indicating that there were no hints available.
Constant Value: 1002 public static final int ACTIVITY_RESULT_OTHER_ACCOUNT Activity result code indicating that the user wishes to use a different account from what was presented in the credential or hint picker.
Constant Value: 1001
https://developers.google.com/android/reference/com/google/android/gms/auth/api/credentials/CredentialsApi#ACTIVITY_RESULT_NO_HINTS_AVAILABLE
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With