所以我一直在试图解决这个问题,但是我似乎不知道出了什么问题。我有一个按钮,单击该按钮时会调用PhoneAuthProvider,它具有4个选项:OnVerifiicationCompleted,OnVerificationFailed,OnCodeSent和onCodeAutoRetrieval。问题是调用了oncodesent,但是我放入其中的代码不起作用,甚至调试日志也无效。我得到的只是一条显示验证码的SMS消息。当我将字符串值设置为onCodeSent的VerificationID时,字符串为value:null。这是我的代码:

sendLink.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            popSound.start();

            if (greenCheck.getVisibility() == View.VISIBLE) {
                //send link

                // we will finish this activity and send the link to the number
                // an option to resend code to the number: it will be provided at the link
                // in this format: resend code to (XXX)-XxX-XXXX
                String number = phoneNumber.getText().toString();
                phoneNumber.setText("");
                sendLink.setEnabled(false);

                PhoneAuthProvider.getInstance().verifyPhoneNumber(
                        number,
                        60,
                        TimeUnit.SECONDS,
                        MobileNumberActivity.this,
                        new PhoneAuthProvider.OnVerificationStateChangedCallbacks() {
                            @Override
                            public void onVerificationCompleted(PhoneAuthCredential phoneAuthCredential) {
                                signInWithPhoneAuthCredential(phoneAuthCredential);
                            }

                            @Override
                            public void onVerificationFailed(FirebaseException e) {

                            }

                            @Override
                            public void onCodeSent(String s, PhoneAuthProvider.ForceResendingToken forceResendingToken) {
                                super.onCodeSent(s, forceResendingToken);
                                // verificationID never gets assigned s and it is null. log.d isn't on logcat
                                verificationID = s;
                                Log.d("testing", "onCodeSent: " + verificationID);
                            }

                            @Override
                            public void onCodeAutoRetrievalTimeOut(String s) {
                                super.onCodeAutoRetrievalTimeOut(s);
                            }
                        }
                );


                Intent i = new Intent(MobileNumberActivity.this, VerificationActivity.class);
                startActivity(i);

                finish();
            } else if (phoneNumber.getText().toString().length() == 0) {
                Toast.makeText(MobileNumberActivity.this, "Please enter a phone number", Toast.LENGTH_SHORT).show();
            } else {
                Toast.makeText(MobileNumberActivity.this, "Please enter a valid phone number", Toast.LENGTH_SHORT).show();
            }
        }
    });

最佳答案

经过反复试验终于找到答案。我不知道为什么会这样,但是我的猜测是因为自从我创建PhoneAuthProvider的实例以来,它仅在oncreate中起作用。我在oncreate中使用了我的工具,但是如上面的代码所示,它被setOnClickListener包围。因此,对我有用的是开始将其定向到我的验证活动,该活动通过oncreate方法自行创建了PhoneAuthProvider的实例,并且该实例有效。

第一次活动:

sendLink.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            popSound.start();

            if (greenCheck.getVisibility() == View.VISIBLE) {
                //send link

                number = phoneNumber.getText().toString();
                phoneNumber.setText("");
                sendLink.setEnabled(false);



                Intent i = new Intent(MobileNumberActivity.this, VerificationActivity.class);
                startActivity(i);



                finish();
            } else if (phoneNumber.getText().toString().length() == 0) {
                Toast.makeText(MobileNumberActivity.this, "Please enter a phone number", Toast.LENGTH_SHORT).show();
            } else {
                Toast.makeText(MobileNumberActivity.this, "Please enter a valid phone number", Toast.LENGTH_SHORT).show();
            }
        }
    });


第二项活动:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_verification);


    mAuth = FirebaseAuth.getInstance();



    mCallbacks = new PhoneAuthProvider.OnVerificationStateChangedCallbacks() {

        @Override
        public void onVerificationCompleted(PhoneAuthCredential credential) {
            // This callback will be invoked in two situations:
            // 1 - Instant verification. In some cases the phone number can be instantly
            //     verified without needing to send or enter a verification code.
            // 2 - Auto-retrieval. On some devices Google Play services can automatically
            //     detect the incoming verification SMS and perform verification without
            //     user action.
            Log.d("COmpleted", "onVerificationCompleted:" + credential);

            signInWithPhoneAuthCredential(credential);
        }

        @Override
        public void onVerificationFailed(FirebaseException e) {
            // This callback is invoked in an invalid request for verification is made,
            // for instance if the the phone number format is not valid.
            Log.w("failed", "onVerificationFailed", e);

            if (e instanceof FirebaseAuthInvalidCredentialsException) {
                // Invalid request
                // ...
            } else if (e instanceof FirebaseTooManyRequestsException) {
                // The SMS quota for the project has been exceeded
                // ...
            }

            // Show a message and update the UI
            // ...
        }

        @Override
        public void onCodeSent(String verificationId,
                               PhoneAuthProvider.ForceResendingToken token) {
            // The SMS verification code has been sent to the provided phone number, we
            // now need to ask the user to enter the code and then construct a credential
            // by combining the code with a verification ID.
            Log.d("codesent", "onCodeSent:" + verificationId);

            // Save verification ID and resending token so we can use them later
            mVerificationID = verificationId;
            mResendToken = token;

            // ...
        }
    };


    PhoneAuthProvider.getInstance().verifyPhoneNumber(
            MobileNumberActivity.number,        // Phone number to verify
            60,                 // Timeout duration
            TimeUnit.SECONDS,   // Unit of timeout
            this,               // Activity (for callback binding)
            mCallbacks);
}

10-04 10:10