使用 GoogleApiClient 实现自动短信验证 Android

Using GoogleApiClient to implement Automatic SMS Verification Android

我正在尝试使用 GoogleApiClient 在片段中实施自动 SMS 验证 Android。我正在关注这里的官方文档 Automatic SMS Verification with the SMS Retriever API

卡在这部分了Obtaining user phone number

下面是我的代码以及我如何在 Android 中尝试使用一个片段来实现,其中我有一个编辑文本 TextInputEditText 用于 phone number

我的片段声明

    TextInputEditText etOtpPhoneNumber;

    GoogleApiClient googleApiClient;

    private static final int RESOLVE_HINT = 1;

我的 Fragment 实现了以下内容

public class smsRetriever extends Fragment implements GoogleApiClient.OnConnectionFailedListener,
        GoogleApiClient.ConnectionCallbacks

在 xml 中查找我的视图并初始化 GoogleApiClient


        etOtpPhoneNumber = view.findViewById(R.id.et_otp_phone_number);


        googleApiClient = new GoogleApiClient.Builder(requireActivity())
                .addConnectionCallbacks(this)
                .enableAutoManage(requireActivity(), this)
                .addApi(Auth.CREDENTIALS_API)
                .build();

我的方法

    @Override
    public void onConnectionFailed(@NonNull ConnectionResult connectionResult) {

    }

    @Override
    public void onConnected(@Nullable Bundle bundle) {

    }

    @Override
    public void onConnectionSuspended(int i) {

    }

    // Obtain the phone number from the result
    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == RESOLVE_HINT) {
            if (resultCode == RESULT_OK) {
                Credential credential = data.getParcelableExtra(Credential.EXTRA_KEY);
                // credential.getId();  <-- will need to process phone number string
            }
        }
    }

我正在实施以下方法来获取用户 phone 号码

有问题

private void requestPhoneNumberHint() {

        HintRequest hintRequest = new HintRequest.Builder()
                .setPhoneNumberIdentifierSupported(true)
                .build();


        PendingIntent intent = Auth.CredentialsApi.getHintPickerIntent(
                googleApiClient, hintRequest);



        try {

            startIntentSenderForResult(intent.getIntentSender(),
                    RESOLVE_HINT, null, 0, 0, 0);

        }catch (IntentSender.SendIntentException e){

            e.printStackTrace();

        }

    }

问题出在 startIntentSenderForResult 上,我遇到以下错误

Expected 7 arguments but found 6

根据 startIntentSenderForResult 方法应该实现以下内容

public void startIntentSenderForResult(@SuppressLint("UnknownNullness") IntentSender intent,
            int requestCode, @Nullable Intent fillInIntent, int flagsMask, int flagsValues,
            int extraFlags, @Nullable Bundle options)

我应该为@Nullable Bundle 选项添加什么


startIntentSenderForResult(pendingIntent.getIntentSender(), CREDENTIAL_PICKER_REQUEST, null,
                    0, 0, 0, new Bundle());


startIntentSenderForResult(intent.getIntentSender(),
                    RESOLVE_HINT, null, 0, 0, 0, null);