简体   繁体   English

如何设置 Android Firebase 电话身份验证的回调?

[英]How to set the callbacks for Android Firebase Phone Authentication?

Im kinda new to firebase and android then I saw this new feature in android the firebase phone Auth.我对 firebase 和 android 有点陌生,然后我在 android firebase phone Auth 中看到了这个新功能。 I was looking at this doucumentation Firebase Phone Auth and I am confused on Implementing this mCallbacks .. can someone pls guide me ?我正在查看这个文档Firebase Phone Auth并且我对实现这个 mCallbacks 感到困惑..有人可以指导我吗?

PhoneAuthProvider.getInstance().verifyPhoneNumber(
                        phoneNum,
                        60,
                        TimeUnit.SECONDS,
                        this,
                        mCallbacks

this confused me is I dont know what data type should I use to assign that callback.这让我很困惑,我不知道应该使用什么数据类型来分配该回调。 since there is no sample code I wish someone might able to guide me.由于没有示例代码,我希望有人能够指导我。

I believe this solved my problem.我相信这解决了我的问题。

submit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String phoneNum = editText.getText().toString();
                Toast.makeText(MainActivity.this, phoneNum, Toast.LENGTH_SHORT).show();
                verifyPhone(phoneNum,mCallBacks);
            }

    });

I tried to make a method to handle the button clicked, I dont know why but it worked..我试图制作一种方法来处理单击的按钮,我不知道为什么,但它有效..

 public void verifyPhone(String phoneNumber, PhoneAuthProvider.OnVerificationStateChangedCallbacks mCallbacks){
            PhoneAuthProvider.getInstance().verifyPhoneNumber(
                    phoneNumber,        // Phone number to verify
                    60,                 // Timeout duration
                    TimeUnit.SECONDS,   // Unit of timeout
                    this,               // Activity (for callback binding)
                    mCallbacks);        // OnVerificationStateChangedCallbac
        }

You should use a PhoneAuthProvider.OnVerificationStateChangedCallbacks() .您应该使用PhoneAuthProvider.OnVerificationStateChangedCallbacks() Like so:像这样:

    PhoneAuthProvider.getInstance().verifyPhoneNumber(
            phoneNumber,        // Phone number to verify
            60,                 // Timeout duration
            TimeUnit.SECONDS,   // Unit of timeout
            this,               // Activity (for callback binding)
            new PhoneAuthProvider.OnVerificationStateChangedCallbacks() {
                @Override
                public void onVerificationCompleted(PhoneAuthCredential phoneAuthCredential) {

                }

                @Override
                public void onVerificationFailed(FirebaseException e) {

                }
            });

Then you could just override the other Verification callbacks that you need.然后你可以覆盖你需要的其他验证回调

不要在 verifyPhoneNumber(..) 方法中传递“this”,而是尝试传递 Activityname.this

方法 verifyPhoneNumber 中 TimeUnit.SECONDS 的问题您必须替换 import Class TimeUnit 以import java.util.concurrent.TimeUnit

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM