Blog Infos
Author
Published
Topics
, ,
Author
Published
Posted By: Saurabh Pant

It holds value until deprecated!

This is a quick and short post on something for which I banged my head on the wall.

While integrating the Phone Selector Api which so far uses an intent sender to show the phone numbers list with the following code is now deprecated.

try {
    startIntentSenderForResult(intent.getIntentSender(),
            RESOLVE_HINT, null, 0, 0, 0,null);
} catch (IntentSender.SendIntentException e) {
    e.printStackTrace();
}

 

On the other hand, the result that we receive in the callback of activity result is deprecated too.

 

@Override
public void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
    super.onActivityResult(requestCode, resultCode, data);

So what’s the fix?

Well, with new ActivityResult Api it is much more simpler.

Firstly, to open the phone number dialog using Phone Selector Api, follow the code

fun getPhoneHintIntent(activity: FragmentActivity): PendingIntent {
    val hintRequest = HintRequest.Builder()
        .setPhoneNumberIdentifierSupported(true)
        .build()

    val options = CredentialsOptions.Builder()
        .forceEnableSaveDialog()
        .build()

    return Credentials.getClient(activity, options).getHintPickerIntent(hintRequest)
}

 

And then in your activity/fragment, use this intent as follows

 

val intent = getPhoneHintIntent(activity as FragmentActivity)
try {
    val launcher = registerForActivityResult(ActivityResultContracts.StartIntentSenderForResult()) {
        if (it.resultCode == Activity.RESULT_OK) {
            val credential: Credential? = it.data?.getParcelableExtra(Credential.EXTRA_KEY)
            print(credential?.id)
        }
    }
    launcher.launch(IntentSenderRequest.Builder(intent).build())
} catch (e: IntentSender.SendIntentException) {
    e.printStackTrace()
}

Job Offers

Job Offers


    Senior Android Engineer – Big Release Team

    Zalando SE
    Berlin
    • Full Time
    apply now

    Lead Android Engineer

    ASOS
    London
    • Full Time
    apply now

    Developer (m/w/d) Backend/ Mobile

    Payback GmbH
    Cologne, Germany
    • Full Time
    apply now
Load more listings

OUR VIDEO RECOMMENDATION

Behind the Curtains

All smartphones have cameras, and we know we can use specific APIs to get amazing shots. But are they the best cameras? Probably not! What if we wanted to drive an external camera, much more powerful than a smartphone? How would we connect to it, and how would we trigger a shot? This and much more…
READ MORE

Jobs

Here, the launch() method takes in an IntentSenderRequest object in which we pass our intent object. In the callback, we can extract the credential data.

And that is it. We’re good to use the Phone Selector Api for better user experiences with new ActivityResult Api in Kotlin.

Here is the final result:

Hope it’ll help.

Keep Coding

Cheers!

YOU MAY BE INTERESTED IN

YOU MAY BE INTERESTED IN

blog
Nowadays authentication has become common in almost all apps. And many of us know…
READ MORE
blog
Collections are a set of interfaces and classes that implement highly optimised data structures.…
READ MORE
blog
Hi, today I come to you with a quick tip on how to update…
READ MORE
blog

How to animate BottomSheet content using Jetpack Compose

Early this year I started a new pet project for listening to random radio…
READ MORE

Leave a Reply

Your email address will not be published. Required fields are marked *

Fill out this field
Fill out this field
Please enter a valid email address.

Menu