Programmatically Obtain the Phone Number of the Android Phone

Programmatically obtain the phone number of the Android phone

Code:

TelephonyManager tMgr = (TelephonyManager)mAppContext.getSystemService(Context.TELEPHONY_SERVICE);
String mPhoneNumber = tMgr.getLine1Number();

Required Permission:

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

Caveats:

According to the highly upvoted comments, there are a few caveats to be aware of. This can return null or "" or even "???????", and it can return a stale phone number that is no longer valid. If you want something that uniquely identifies the device, you should use getDeviceId() instead.

How to get the mobile number of my device programmatically?

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

Get phonenumber programmatically - Android

The method you are using is the only one part of the SDK to do this, and only works on devices where the number is stored on the SIM card, which only some carriers do. For all other carriers, you will have to ask the user to enter the phone number manually, as the number is simply not stored anywhere on the device from where you can retrieve it.

to get phone number programmatically in Android

I think Sim serial Number is unique. You can try this.

TelephonyManager telemamanger = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);
String getSimSerialNumber = telemamanger.getSimSerialNumber();

Let me know if there is any issue.

How to get Mobile phone Number using flutter Framework?

There is no cross-platform solution for that as Apple restricts its iOS Apps and prohibit the access of the phone number. The only thing you can do is write a package which only supports Android and returns the phone number. Refer to:

Programmatically get own phone number in iOS

Get the device's phone number programmatically

For Android:

Programmatically obtain the phone number of the Android phone

Create a flutter package

Edit 2019/09/03:

Some apps like Snapchat bypass Apple's restriction by sending a sms from the users phone to a server which then returns the phone number. Here is a blog post about doing this with AWS:

Capturing Mobile Phone Numbers



Related Topics



Leave a reply



Submit