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


    Flutter Developers and Architects

    Rebell App Studio by Codemate
    Helsinki, Oulu, Bangkok
    • Full Time
    apply now

    Developer Relations Engineer

    Embrace
    United States
    • Full Time
    apply now

    Information Security Engineer

    MongoDB
    London, UK
    • Full Time
    apply now
Load more listings

OUR VIDEO RECOMMENDATION

,

Monetizing your Flutter App

How can you smartly integrate advertising and in-app purchases to monetize your Flutter app? Using the popular word game 4 Pics 1 Word as an example, we will explore the basic procedure and best practices…
Watch Video

Monetizing your Flutter App

Petra Langenbacher & Joachim Böhmer
Software Developer
Lotum

Monetizing your Flutter App

Petra Langenbacher ...
Software Developer
Lotum

Monetizing your Flutter App

Petra Langenbach ...
Software Developer
Lotum

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
It’s one of the common UX across apps to provide swipe to dismiss so…
READ MORE
blog
Hi, today I come to you with a quick tip on how to update…
READ MORE
blog
Automation is a key point of Software Testing once it make possible to reproduce…
READ MORE
blog
Drag and Drop reordering in Recyclerview can be achieved with ItemTouchHelper (checkout implementation reference).…
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