Android Phone Number Verification | Phone Selector API to Get the Number | SMS Verification API to Verify number

Hello Friends, Now a days it is very common approach to verify user’s mobile number while authenticating user. Beginner Android developer might think to send OTP then verify by comparing but there are many problems in sending OTP. Specially for those who has not income for the app they might not be interested in buying paid SMS.

Some developers also think to use third party libraries such as Firebase Phone Authentication , but the problem I face with Firebase Phone Authentication is it is not working if the number is ported from one provider to another.

So I was looking for the solution like what other ways we can get valid phone number of user and I come to know about Phone Selector API provided by Google for getting phone number and continue to app without OTP. Using below image we can understand more clearly what I am talking about. I took reference of of this image from android developer blog.

So Here I am providing steps to implement Phone Selector API in android project

implementation ‘’

The dependency we have to add to enable Google Account Login, you can find latest version of this dependency from developer google guides.

private void requestHint() {
HintRequest hintRequest = new HintRequest.Builder()
CredentialsOptions options = new CredentialsOptions.Builder()
PendingIntent intent = Credentials.getClient(getContext(), options)

try {
RESOLVE_HINT, null, 0, 0, 0,null);
} catch (IntentSender.SendIntentException e) {

Above code will help to display dialog with phone number available inside device running your app and user can select any number he would like to continue with.

public void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    if (requestCode == RESOLVE_HINT) {
        if (resultCode == RESULT_OK) {
            Credential credential = data.getParcelableExtra(Credential.EXTRA_KEY);
            String mobileNumber=credential.getId();

onActivityResult() method will help to get the number user has selected and then you can write next logic to continue with your app.

With this code you can complete your code of mobile number verification but if you want to verify ownership of number then we have to send OTP and verify using following code.

SMS Verification API

Now we have detected phone number available in device using Phone Selector API but it also very important to verify the ownership of the number.

Without ownership verification user can use other user’s device or number which is not in his/her ownership. So by sending one time password we can have more strongly confirmation about mobile number with valid user.

For sending SMS as we discussed earlier we have to buy SMS Api or we can also use Firebase Phone Authentication.

Using SMS verfication API we can have automatically phone number verification but for that we have to follow some instructions given by google and write code on client side.

We have to prepare our message with One Time Password and 11-character hash string that identifies our app. For more details about hash string you can read on Developer Android Documentation

As shown in above image our message might be anything, OTP position we can set anything but the message must end with 11-character hash string. This hash string used by SMS verification API code given below.

SmsRetrieverClient client = SmsRetriever.getClient(this /* context */);

Task<Void> task = client.startSmsRetriever();

task.addOnSuccessListener(new OnSuccessListener<Void>() {
  public void onSuccess(Void aVoid) {
    // successfully started an SMS Retriever for one SMS message

task.addOnFailureListener(new OnFailureListener() {
  public void onFailure(@NonNull Exception e) {

SmsRetrieverClient we can set as soon as we are ready with phone number and once we start this client it will wait for 5 min for SMS message which contains hash string.

Once it receive message with hash string Play service broadcast intent named SmsRetriever.SMS_RETRIEVED_ACTION with text message we sent from server. So we have to use following Broadcast Receiver to receive verification message.

public class SMSBroadcastReceiver extends BroadcastReceiver {

  public void onReceive(Context context, Intent intent) {
    if (SmsRetriever.SMS_RETRIEVED_ACTION.equals(intent.getAction())) {
      Bundle extras = intent.getExtras();
      Status status = (Status) extras.get(SmsRetriever.EXTRA_STATUS);

      switch(status.getStatusCode()) {
        case CommonStatusCodes.SUCCESS:
          String message = (String) extras.get(SmsRetriever.EXTRA_SMS_MESSAGE);
        case CommonStatusCodes.TIMEOUT:

From message we have to extract verification code and confirm the ownership.

I hope this API would increase user experience with your application and they easily complete sign up process.

If you like this post please write comment also can ask any query and follow me on github page.

Related Posts

2 thoughts on “Android Phone Number Verification | Phone Selector API to Get the Number | SMS Verification API to Verify number

Leave a Reply