Table of contents
The Phone Number Hint API is a part of the Google Play services library and uses a PendingIntent
to initiate the flow. This means that the user is presented with a UI that lists all of their SIM-based phone numbers. The user can then select the phone number they would like to use, or they can cancel the flow. The selected phone number is then made available to you to retrieve from the Intent.
The main reasons to use Phone Number Hint API includes:
Extra added functionality
No additional permissions required
Eliminates the need for the user to manually type in the phone number
No Google account is needed
Dependency:
dependencies {
implementation 'com.google.android.gms:play-services-auth:20.6.0'
}
Implementation:
- As mentioned above, we need to send a PendingIntent and we will do it by first registering a “phoneNumberHintIntentResultLauncher”.
val phoneNumberHintIntentResultLauncher =
//Only used inside of a composable(for outside use registerForActivityResult instead)
rememberLauncherForActivityResult(
contract = ActivityResultContracts.StartIntentSenderForResult()
) { result ->
//Here, we will get the Intent Result(Either cancelled or selected)
try {//Make sure to handle any exceptions
//Get phone number from result(See Step 2.)
} catch (e: Exception) {
Log.e(TAG, "Phone Number Hint failed")
}
}
2. Now, If the user has selected any phone number, then we need to fetch it.
try{
val phoneNumberHint = Identity//The entry point to the Sign-In APIs.
.getSignInClient(context as Activity)
.getPhoneNumberFromIntent(result.data)//Get phone number from Sign-In client
postalCode = phoneNumberHint.substring(0, 3)//First 2,3 digits are postal code
phoneNumber = phoneNumberHint.substring(3)
focusRequester.freeFocus()//see futher steps
//Do anything with phone number
}
3. Now since we have a launcher ready, we can launch it whenever you want the user to see a pop-up for Phone Hint. In my case, it was whenever the user clicks on the TextField.
To trigger the launch, first I need to check when the user clicked on the TextField
I added a FocusRequester to my TextField.
Hit the launch in onFocusChanged
val focusRequester = remember { FocusRequester() }
.
.
.
//In any of your TextField...
modifier = Modifier
.focusRequester(focusRequester)
.onFocusChanged {
if (it.isFocused) {
getPhoneNumberHint()//Complete method in next step.
}
}
4. Now, we will create the actual request Intent that we wil send into our launcher.
val getPhoneNumberHint = {
val request: GetPhoneNumberHintIntentRequest =//Request object used to get an Intent to start the Phone Number Hint flow
GetPhoneNumberHintIntentRequest.builder().build()
try {
Identity.getSignInClient(context as Activity)
.getPhoneNumberHintIntent(request)
.addOnSuccessListener { result: PendingIntent ->
try {
phoneNumberHintIntentResultLauncher.launch(
IntentSenderRequest.Builder(result).build()
)//Success: means the user can see the Hints(now handle the result in launcher)
} catch (e: Exception) {
Log.e("Phone", "Launching the PendingIntent failed")
}
}
.addOnFailureListener {
Log.e("Phone", "Phone Number Hint failed")
}
} catch (e: Exception) {
Log.e("Phone", "PhoneLoginScreen: Phone hint exception")
}
}
That’s it!
Let me explain the complete flow….
User clicks on the TextField → onFocusChanged is called → Calls getPhoneNumberHint → Create a request → on Success launch the IntentResultLauncher → get the user action result in phoneNumberHintIntentResultLauncher(make sure to register it before all this).
I hope you found this helpful. If yes, then do FOLLOW ‘Sagar Malhotra’ for more Android-related content.
#androidWithSagar #android #androiddevelopment #development #compose #kotlin