Phone Number Hint API in Jetpack Compose
Sagar Malhotra
Android dev by day, Creator by night.???? Sharing my passion through videos, blogs and conferences.??
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:
Dependency:
dependencies {
implementation 'com.google.android.gms:play-services-auth:20.6.0'
}
Implementation:
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.
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.