banner image

Phone number suggestion in Xamarin Android

Phone Number Suggestion in Xamarin Android



Building an android application using xamarin forms or xamarin android no matter what ever framework you are using.But xamarin android it's likely Native framework similar like Java/Kotlin.

Now coming to the actual point,whenever developers implements Google Phone Auth for authenticate the user using phone no.If you didn't implement OTP verification with auto read OTP using SMS Retriver API you can see my article Read OTP in Xamarin Android .

So whenever you are going to implement SMS retriver API,a important point coming to the developer mind,why not we suggest the user phone during phone authtication?

few years ago it was few lines of code to get the user phone no from the device using Telephony Manager,but believe me,it will not work as expected.Because the telecom service provider never keep the phone number inside the sim card.So using the Telephony Manager you will not get the user phone no.It will returns null or blank data.So the below code will works for showing the phone number suggestion in a small dialog.Now I am telling you,how its works? Generally in the android device we have to login through a Gmail account.So when you logged in to the gmail account and we attach any phone number along with gmail account,google keeps your information and phone no.






Before using this code,you must install this two library to your android project:
Xamarin.GooglePlayServices.Auth
Xamarin.GooglePlayServices.Auth.Api.Phone

and do not forget to use GooglePlayServices.Json file inside your android project,I will recommend you to watch my Read OTP in Xamarin Android video.
   int hintResultCode = 100;
   
    private void TestPhoneHint()
        {
            try
            {
                HintRequest hintRequest = new HintRequest.Builder().SetPhoneNumberIdentifierSupported(true).Build();

                GoogleApiClient googleApiClient = new GoogleApiClient.Builder(this).AddApi(Auth.CREDENTIALS_API).Build();

                PendingIntent intent = Auth.CredentialsApi.GetHintPickerIntent(googleApiClient, hintRequest);
                StartIntentSenderForResult(intent.IntentSender, hintResultCode, null, 0, 0, 0);
            }
            catch (Exception ex)
            {
                
            }
        }

This code will open a phone no hint intent,when use select any phone no from the list,then OnActivityResult will call and you will get the selected phone no as E164 Format 
        
    protected override void OnActivityResult(int requestCode, Result resultCode, Intent data)
        {
            if(requestCode == hintResultCode)
            {
                var credential = (Credential)data.GetParcelableExtra(Credential.ExtraKey);
                string selectedPhoneNo = credential.Id;
            }
        }

 Lets try the code and see it works or not.Keep coding and be happy. :)
Phone number suggestion in Xamarin Android Phone number suggestion in Xamarin Android Reviewed by SP Tutorials on June 21, 2021 Rating: 5

2 comments:

  1. You are playing a great role to provide information in people. Many people face this issue and they could not resolved it. From your efforts, They can solve it. For more getting new and amazing gadgets we will be visit website.

    ReplyDelete

Powered by Blogger.