7

I am justing trying to get phone number using GetPhoneNumberHintIntentRequest to replace HintRequest. So just trying to follow google developer doc https://developers.google.com/identity/phone-number-hint/android#kotlin_2. But after following doc I feel this doc is incomplete.

enter image description here

  val phoneNumberHintIntentResultLauncher: ActivityResultLauncher<Intent> =
    registerForActivityResult(ActivityResultContracts.StartActivityForResult()) { result ->
        try {
            val phoneNumber = Identity.getSignInClient(requireActivity()).getPhoneNumberFromIntent(result.data)
        } catch(e: Exception) {
        }
  }

So as per doc you need to pass intent to phoneNumberHintIntentResultLauncher but there is no method inside GetPhoneNumberHintIntentRequest.

enter image description here

Even if you see doc then you realise that you need to replace signInClient to getSignInClient.

If any one know about above issue then let me know or any doc where I can achieve my goal.

duggu
  • 37,851
  • 12
  • 116
  • 113
  • Me too facing the same issue when I tried to update the deprecated code of the phone selector API. It does seem like the doc is incomplete about the new API setup. Please do share the solution if you find it. Thank you! – sHaRkBoY Jun 08 '22 at 12:51
  • Even I am facing the same issue. Please do share the solution. Thanks in advance!!! – Tejaswini Dev Jul 22 '22 at 07:21

2 Answers2

2

Have been facing this recently.

Please change the result launcher type as follows.

    val resultLauncher: ActivityResultLauncher<IntentSenderRequest> = registerForActivityResult(StartIntentSenderForResult()) { result ->
        try {
            val phoneNumber = Identity.getSignInClient(requireActivity()).getPhoneNumberFromIntent(result.data)
            // Do something with the number
        } catch (e: Exception) {
            Log.e(TAG, "Phone Number Hint failed")
        }

And launch the intent as

    ...
    .addOnSuccessListener { request: PendingIntent ->
            try {
                resultLauncher.launch(IntentSenderRequest.Builder(request).build())
            } catch(e: Exception) {
                Log.e(TAG, "Launching the PendingIntent failed")
            }
        }
    ...

The document is indeed incomplete as it seems.

0

// initialise above onCreate

private ActivityResultLauncher<IntentSenderRequest> someActivityResultLauncher; 

// declare in onCreate

private void phoneSelection() {
        GetPhoneNumberHintIntentRequest request =  GetPhoneNumberHintIntentRequest.builder().build();
        Identity.getSignInClient(RegistrationActivity.this)
                .getPhoneNumberHintIntent(request)
                .addOnFailureListener(e -> {
                    Toast.makeText(activity, "Error : "+e.getMessage(), Toast.LENGTH_SHORT).show();
                }).addOnSuccessListener(pendingIntent -> {
                    IntentSenderRequest intentSenderRequest = new IntentSenderRequest.Builder(pendingIntent.getIntentSender()).build();
                    someActivityResultLauncher.launch(intentSenderRequest);
                });
    }

// declare in onCreate

private void resultLauncher() {
        someActivityResultLauncher = registerForActivityResult(new ActivityResultContracts.StartIntentSenderForResult(), result -> {
            try {
                String phoneNumber = Identity.getSignInClient(RegistrationActivity.this).getPhoneNumberFromIntent(result.getData());
                binding.editNumber.setText(phoneNumber.substring(3)); //get the selected phone
            } catch (ApiException e) {
                e.printStackTrace();
                Toast.makeText(activity, "Error : "+ e.getMessage(), Toast.LENGTH_SHORT).show();
            }
        });
    }