9

I am building a custom capacitor plugin to fetch the user's phone numbers. I am using capacitor 3 with Ionic 6.

I found a solution which is not deprecated and is a lot recent to fetch the user's phone numbers.

here is my code to get the phone number -

private void requestHint() {
        HintRequest hintRequest = new HintRequest.Builder()
                .setPhoneNumberIdentifierSupported(true)
                .build();
        PendingIntent intent = Credentials.getClient(getActivity()).getHintPickerIntent(hintRequest);
        IntentSenderRequest.Builder intentSenderRequest = new IntentSenderRequest.Builder(intent.getIntentSender());
        hintLauncher.launch(intentSenderRequest.build());
    }

ActivityResultLauncher<IntentSenderRequest> hintLauncher = registerForActivityResult(new ActivityResultContracts.StartIntentSenderForResult(),
            result -> {
                if(result!=null && result.getData()!=null){
                    Intent data = result.getData();
                    Credential credential = data.getParcelableExtra(Credential.EXTRA_KEY);
                    String phoneNum = credential.getId();
                }
            });

But I am running across an error on Android Studio "Cannot resolve method 'registerForActivityResult' in 'NumberPluginPlugin'"

What am I missing here? As suggested by some folks online I have added the following dependencies -

implementation "androidx.fragment:fragment:1.4.1"
implementation "androidx.activity:activity:1.4.0"
implementation "androidx.appcompat:appcompat:$androidxAppCompatVersion"

Still I am not sure what is going wrong here.

ADM
  • 20,406
  • 11
  • 52
  • 83
K. Ajay
  • 357
  • 1
  • 4
  • 21

5 Answers5

5

registerForActivityResult can be called inside AppCompatActivity or Fragment (when you are using AndroidX), not some custom NumberPluginPlugin class (which doesn't extend the classes mentioned above).

Note that there's no such method in common Activity, only AndroidX extension of Activity brings this feature.

HERE you can find some article about how to implement this new way (also with comparison to the old, deprecated one).

hippietrail
  • 15,848
  • 18
  • 99
  • 158
snachmsm
  • 17,866
  • 3
  • 32
  • 74
  • 1
    Okay. Where shall I implement this code, a class extending a fragment? or is there any other way? – K. Ajay Apr 27 '22 at 11:54
  • yep, in class extending `Activity` or `Fragment`. For fragment you should call it BEFORE `Fragment` attach to `Activity`, e.g. in overriden `onAttach()` method before `super` call – snachmsm Apr 28 '22 at 05:42
  • 2
    i'm calling this inside an Activity and STILL getting the error. I have all the androidx dependencies declared in gradle files still issue. – JCutting8 Jun 06 '22 at 11:48
  • are you shure you are calling this method inside `Activity` and not inside some inner class? is `this` points on `Activity` on some other class? – snachmsm Jun 06 '22 at 12:07
1

try update appcompat to newest version

implementation 'androidx.appcompat:appcompat:1.5.1'
Donald Duck
  • 8,409
  • 22
  • 75
  • 99
mc_hand_os
  • 21
  • 5
1

Use AppCompatActivity instead of Activity

something like : public class ActivityMain extends Activity ----> public class ActivityMain extends AppCompatActivity

Riccardo
  • 1,083
  • 2
  • 15
  • 25
Komang
  • 11
  • 1
0

I had the registerForActivityResult method inside a Fragment and still didnt work. This answer helped me: https://stackoverflow.com/a/66209780/1034622

Pablo Chvx
  • 1,809
  • 18
  • 31
0

you may need to add this (if not exist) into your app build.gradle :

implementation 'androidx.activity:activity-ktx:1.6.0'
Amer Alzibak
  • 1,489
  • 15
  • 16