Programmatically obtain the phone number of the Android phone

android telephonymanager get phone number
android get current phone number programmatically
how to get second sim number in android programmatically
phone selector api android
ios 11 get phone number programmatically
get country code from phone number android programmatically
what is my phone number android
auto detect phone number android

How can I programmatically get the phone number of the device that is running my android app?

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.

Programmatically obtain the phone number of the Android phone , In this tutorial, you will learn to get own phone number or application running device phone number programmatically in Android. Get the user’s phone number as manual input from the user. Send a code to the user’s mobile number via SMS. Ask user to enter the code to confirm the phone number. Save the number in sharedpreference.

There is no guaranteed solution to this problem because the phone number is not physically stored on all SIM-cards, or broadcasted from the network to the phone. This is especially true in some countries which requires physical address verification, with number assignment only happening afterwards. Phone number assignment happens on the network - and can be changed without changing the SIM card or device (e.g. this is how porting is supported).

I know it is pain, but most likely the best solution is just to ask the user to enter his/her phone number once and store it.

How to get the phone number programmatically in Android, This example demonstrate about How to get phone number in android.Step 1 − Create a new project in Android Studio, go to File ⇒ New  This example demonstrates how do I obtain the phone number of the android phone programmatically. Step 1 − Create a new project in Android Studio, go to File ⇒ New Project and fill all required details to create a new project. Step 2 − Add the following code to res/layout/activity_main.xml.

Update: This answer is no longer available as Whatsapp had stopped exposing the phone number as account name, kindly disregard this answer.

There is actually an alternative solution you might want to consider, if you can't get it through telephony service.

As of today, you can rely on another big application Whatsapp, using AccountManager. Millions of devices have this application installed and if you can't get the phone number via TelephonyManager, you may give this a shot.

Permission:

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

Code:

AccountManager am = AccountManager.get(this);
Account[] accounts = am.getAccounts();

for (Account ac : accounts) {
    String acname = ac.name;
    String actype = ac.type;
    // Take your time to look at all available accounts
    System.out.println("Accounts : " + acname + ", " + actype);
}

Check actype for WhatsApp account

if(actype.equals("com.whatsapp")){
    String phoneNumber = ac.name;
}

Of course you may not get it if user did not install WhatsApp, but its worth to try anyway. And remember you should always ask user for confirmation.

How to get phone number in android?, How can I programmatically obtain the number of the Android phone with the API Take your Duration: 2:42 Posted: Oct 27, 2013 How can I programmatically get the phone number of the device that is running my android app?

As posted in my earlier answer

Use below code :

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

In AndroidManifest.xml, give the following permission:

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

But remember, this code does not always work, since Cell phone number is dependent on the SIM Card and the Network operator / Cell phone carrier.

Also, try checking in Phone--> Settings --> About --> Phone Identity, If you are able to view the Number there, the probability of getting the phone number from above code is higher. If you are not able to view the phone number in the settings, then you won't be able to get via this code!

Suggested Workaround:

  1. Get the user's phone number as manual input from the user.
  2. Send a code to the user's mobile number via SMS.
  3. Ask user to enter the code to confirm the phone number.
  4. Save the number in sharedpreference.

Do the above 4 steps as one time activity during the app's first launch. Later on, whenever phone number is required, use the value available in shared preference.

How can I programmatically obtain the number of the Android phone , to programmatically obtain the phone number of the android phone subscriptions that obtained my phone numbers without my consent. In this tutorial, you will learn to get own phone number or application running device phone number programmatically in Android. There are two ways to get phone number in Android. TelephonyManager (from API 1) SubscriptionManager (from API 22) TelephonyManager. Android TelephonyManager provides

So that's how you request a phone number through the Play Services API without the permission and hacks. Source and Full example.

In your build.gradle (version 10.2.x and higher required):

compile "com.google.android.gms:play-services-auth:$gms_version"

In your activity (the code is simplified):

@Override
protected void onCreate(Bundle savedInstanceState) {
    // ...
    googleApiClient = new GoogleApiClient.Builder(this)
            .addApi(Auth.CREDENTIALS_API)
            .build();
    requestPhoneNumber(result -> {
        phoneET.setText(result);
    });
}

public void requestPhoneNumber(SimpleCallback<String> callback) {
    phoneNumberCallback = callback;
    HintRequest hintRequest = new HintRequest.Builder()
            .setPhoneNumberIdentifierSupported(true)
            .build();

    PendingIntent intent = Auth.CredentialsApi.getHintPickerIntent(googleApiClient, hintRequest);
    try {
        startIntentSenderForResult(intent.getIntentSender(), PHONE_NUMBER_RC, null, 0, 0, 0);
    } catch (IntentSender.SendIntentException e) {
        Logs.e(TAG, "Could not start hint picker Intent", e);
    }
}

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    if (requestCode == PHONE_NUMBER_RC) {
        if (resultCode == RESULT_OK) {
            Credential cred = data.getParcelableExtra(Credential.EXTRA_KEY);
            if (phoneNumberCallback != null){
                phoneNumberCallback.onSuccess(cred.getId());
            }
        }
        phoneNumberCallback = null;
    }
}

This will generate a dialog like this:

Possible to programmatically obtain the phone number of the , Returns the phone number string for line 1, for example, the MSISDN for a GSM phone. String · getManualNetworkSelectionPlmn(). Get the PLMN chosen for  Home » Java » Programmatically obtain the phone number of the Android phone Programmatically obtain the phone number of the Android phone Posted by: admin October 25, 2018 Leave a comment

How do I programmatically obtain users' mobile phone number from , To get and display phone number programmatically in android you have to write little bit java code and in this example I am going to show to get  Programmatically obtain the phone number of the Android phone (10) Update: This answer is no longer available as Whatsapp had stopped exposing the phone number as account name, kindly disregard this answer.

TelephonyManager, Phone number assignment happens on the network - and can be changed without changing the SIM card or device (e.g. this is how porting is  Did you add <uses-permission android:name="android.permission.READ_PHONE_STATE"/> to manifest? It's not really perfect, but the only way (at least to my knowledge). Broader discussion is here Programmatically obtain the phone number of the Android phone

How to Get the Phone Number Programmatically in Android, The imei number is used to manage and identify the mobile phone. On android app we can easily get the current mobile device imei number using TelephonyManager class. So here is the complete step by step tutorial for Find / Get imei number in android programmatically.

Comments
  • this is not possible, except if you enter it by your self. The phone number is not know by the mobile or the SIM, only by the network....
  • @tomsoft Think about it... How do apps like Facebook auto-magically verify your phone number..?
  • @tomsoft No, most of the time it doesn't even ask for your phone number, or if it does then the box is prefilled.
  • @tomsoft Well, I signed up for Facebook the other day, and it didn't ask for my number when it texted me a code. Try it. ;)
  • @EddieHart Do it also, and I've been asked my phone number. So this is confirm that generally speaking, this is not accessible and some operators might add this info on setup but this is not a GMS standard feature
  • Actually, not so perfect. Last time I tried this method, it reported the phone number that my phone originally had, before my old mobile number was ported over to it. It probably still does, as the Settings app still shows that defunct number. Also, there are reports that some SIMs cause this method to return null. That being said, I'm not aware of a better answer.
  • Mark is right. If the intent to use something that uniquely identifies phone i'd use getDeviceId() which will return IMEA for GSM.
  • (I think you mean IMEI...?)
  • Well, i tested it on Nexus One with Android OS 2.2 and it returns null
  • Developers may think it is the perfect answer by considering the vote count. A note should be added to warn the developer that this method wont give phone number always.