Android developmentAndroid tutorial

How to obtain the phone number of the Android phone Programmatically

How to obtain the phone number of the Android phone Programmatically
How to obtain the phone number of the Android phone Programmatically

Hii In this Android solution we make an example for obtaining the phone number of the Android Phone Programmatically.  Like you can see some professional App automatically get the mobile no and show it to the user in the popup. so let’s How to obtain the phone number of the Android phone Programmatically.

When the customer installs your App and at the login time and registration time automatically get the user mobile no and show to the user select one. in this Android example, we use google Library for getting mobile no.

Let’s Start On Solution 

Add Library in our App build.Gradle

dependencies {
  implementation fileTree(dir: 'libs', include: ['*.jar'])
  implementation 'androidx.appcompat:appcompat:1.0.2'
  implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
  // lib for SMS verification (Phone Auth)
  implementation 'com.google.android.gms:play-services-auth:17.0.0'
  implementation 'com.google.android.gms:play-services-auth-api-phone:17.0.0'
  implementation 'com.google.android.gms:play-services-identity:17.0.0'
  implementation 'com.google.android.gms:play-services-base:17.3.0'
  testImplementation 'junit:junit:4.12'
  implementation 'androidx.appcompat:appcompat:1.0.0'
  androidTestImplementation 'androidx.test.ext:junit:1.1.1'
  androidTestImplementation 'androidx.test.espresso:espresso-core:3.1.0'
}

after adding this library sync your project and Implement this code.

MainActivity.java code  

Add these codes whare you want to implement, get a phone number in android. This is a very esay Android example How to get a phone number on android?. Let’s Just copy and paste this code.

public class MainActivity2 extends AppCompatActivity {


    private static final int CREDENTIAL_PICKER_REQUEST = 1;
    EditText EditText;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main2);
        EditText = findViewById(R.id.EditText);
        HintRequest hintRequest = new HintRequest.Builder()
                .setPhoneNumberIdentifierSupported(true)
                .build();


        PendingIntent intent = Credentials.getClient(this).getHintPickerIntent(hintRequest);
        try
        {
            startIntentSenderForResult(intent.getIntentSender(), CREDENTIAL_PICKER_REQUEST, null, 0, 0, 0,new Bundle());
        }
        catch (IntentSender.SendIntentException e)
        {
            e.printStackTrace();
        }
    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data)
    {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == CREDENTIAL_PICKER_REQUEST && resultCode == RESULT_OK)
        {
            // Obtain the phone number from the result
            Credential credentials = data.getParcelableExtra(Credential.EXTRA_KEY);
            EditText.setText(credentials.getId().substring(3)); //get the selected phone number
//Do what ever you want to do with your selected phone number here


        }
        else if (requestCode == CREDENTIAL_PICKER_REQUEST && resultCode == CredentialsApi.ACTIVITY_RESULT_NO_HINTS_AVAILABLE)
        {
            // *** No phone numbers available ***
            Toast.makeText(MainActivity2.this, "No phone numbers found", Toast.LENGTH_LONG).show();
        }


    }
}