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 Compiler Engineer C++/LLVM – Munich

    Guardsquare
    Munich
    • Full Time
    apply now

    Android Engineer

    American Express
    New York
    • Full Time
    apply now

    Mobile Developer Android (m/w/d) Firebase

    REWE digital
    Köln / Cologne
    • Full Time
    apply now
Load more listings

OUR VIDEO RECOMMENDATION

, ,

The Evolution of Android Graphics in Android 12/13

Android 12 and 13 both added significant new capabilities to Android platform graphics, including RenderEffect, RuntimeShader, and more. At the same time, RenderScript has been deprecated and we’ve introduced the RenderScript Intrinsics Replacement Toolkit. This…
Watch Video

The Evolution of Android Graphics in Android 12/13

Daniel Galpin
Android Developer Advocate and Fast Talking YouTuber
Google

The Evolution of Android Graphics in Android 12/13

Daniel Galpin
Android Developer Ad ...
Google

The Evolution of Android Graphics in Android 12/13

Daniel Galpin
Android Developer Advocat ...
Google

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.

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

Menu