java - 在 Android Fragment 中使用 Firebase 电话身份验证

标签 java android firebase android-fragments firebase-realtime-database

我正在制作一个 Android 应用程序,其中唯一的用户登录和注册方法是通过电话身份验证。我正在进行一项 Activity 并使用 fragment 。

我想在 SignInFragment 中添加 Firebase 电话身份验证并使用 Firebase PhoneAuthProvider。但是,这仅适用于我认为的 Activity ,因为我尝试在我的 fragment 中实现,但我的应用程序崩溃了。

这是SignInFragment.java的代码

package com.example.XX;

import android.os.Bundle;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;

import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;

import com.google.android.gms.tasks.OnCompleteListener;
import com.google.android.gms.tasks.Task;
import com.google.firebase.FirebaseException;
import com.google.firebase.auth.AuthResult;
import com.google.firebase.auth.FirebaseAuth;
import com.google.firebase.auth.FirebaseAuthInvalidCredentialsException;
import com.google.firebase.auth.FirebaseUser;
import com.google.firebase.auth.PhoneAuthCredential;
import com.google.firebase.auth.PhoneAuthProvider;

import java.util.concurrent.Executor;
import java.util.concurrent.TimeUnit;

public class SignInFragment extends Fragment {

    private static final String TAG ="INSIDE_SIGN_IN_FRAGMENT" ;
    FirebaseAuth mAuth;
    EditText xPhone;
    EditText xOTP;
    String codeSent;

    public SignInFragment() {
        // Required empty public constructor
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        return inflater.inflate(R.layout.fragment_sign_in, container, false);
    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);

        Button getOTP=view.findViewById(R.id.button_xOTP);
        Button submitButton=view.findViewById(R.id.signInbutton);
        xPhone=view.findViewById(R.id.editText_xMobile);
        xOTP=view.findViewById(R.id.editText_xOTP);

        mAuth=FirebaseAuth.getInstance();




        getOTP.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                sendVerificationCode();
            }
        });

        submitButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                verifyOTP();
            }
        });
    }

    private void verifyOTP(){
        String tOTP=xOTP.getText().toString().trim();

        if(tOTP.isEmpty()){
            xOTP.setError("OTP is required!");
            xOTP.requestFocus();
            return;
        }

        if(tOTP.length()<6){
            xOTP.setError("Please enter a valid OTP!");
            xOTP.requestFocus();
            return;
        }
        PhoneAuthCredential credential = PhoneAuthProvider.getCredential(codeSent, tOTP);
        signInWithPhoneAuthCredential(credential);
    }

    private void signInWithPhoneAuthCredential(PhoneAuthCredential credential) {
        mAuth.signInWithCredential(credential)
                .addOnCompleteListener((Executor) this, new OnCompleteListener<AuthResult>() {
                    @Override
                    public void onComplete(@NonNull Task<AuthResult> task) {
                        if (task.isSuccessful()) {
                            // Sign in success, update UI with the signed-in user's information
                            Log.d(TAG, "signInWithCredential:success");

                            FirebaseUser user = task.getResult().getUser();
                            // ...
                        } else {
                            // Sign in failed, display a message and update the UI
                            Log.w(TAG, "signInWithCredential:failure", task.getException());
                            if (task.getException() instanceof FirebaseAuthInvalidCredentialsException) {
                                // The verification code entered was invalid
                            }
                        }
                    }
                });
    }

    private void sendVerificationCode() {

        String xMobileNumber=xPhone.getText().toString().trim();

        if(xMobileNumber.isEmpty()){
            xPhone.setError("Phone number is required!");
            xPhone.requestFocus();
            return;
        }

        if(xMobileNumber.length()<10){
            xPhone.setError("Please enter a valid phone number!");
            xPhone.requestFocus();
            return;
        }
        PhoneAuthProvider.getInstance().verifyPhoneNumber(
                xMobileNumber,        // Phone number to verify
                60,                 // Timeout duration
                TimeUnit.SECONDS,   // Unit of timeout
                (Executor) this,               // Activity (for callback binding) //NOT SURE ABOUT THIS
                mCallbacks);        // OnVerificationStateChangedCallbacks

    }

    PhoneAuthProvider.OnVerificationStateChangedCallbacks mCallbacks=new PhoneAuthProvider.OnVerificationStateChangedCallbacks() {
        @Override
        public void onVerificationCompleted(@NonNull PhoneAuthCredential phoneAuthCredential) {

        }

        @Override
        public void onVerificationFailed(@NonNull FirebaseException e) {

        }

        @Override
        public void onCodeSent(@NonNull String s, @NonNull PhoneAuthProvider.ForceResendingToken forceResendingToken) {
            super.onCodeSent(s, forceResendingToken);
            codeSent=s;
        }
    };


}

我的Logcat

LOGCAT:2020-06-10 03:59:44.148 16491-16491/com.example.XX
 E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.example.XX, PID: 16491
    java.lang.ClassCastException: com.example.XX.SignInFragment cannot be cast to java.util.concurrent.Executor
        at com.example.XX.SignInFragment.sendVerificationCode(SignInFragment.java:132)
        at com.example.XX.SignInFragment.access$000(SignInFragment.java:29)
        at com.example.XX.SignInFragment$1.onClick(SignInFragment.java:65)
        at android.view.View.performClick(View.java:7201)
        at android.view.View.performClickInternal(View.java:7170)
        at android.view.View.access$3500(View.java:806)
        at android.view.View$PerformClick.run(View.java:27582)
        at android.os.Handler.handleCallback(Handler.java:883)
        at android.os.Handler.dispatchMessage(Handler.java:100)
        at android.os.Looper.loop(Looper.java:214)
        at android.app.ActivityThread.main(ActivityThread.java:7695)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:516)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:950)

我知道问题出在执行器部分,IDE建议在两个地方使用执行器。也许 PhoneAuth 方法不适用于 fragment 。 任何人都可以查看此内容或告知仅在 fragment 内实现 Firebase 电话身份验证的替代方法。

最佳答案

this 关键字在 Activity 中使用。

在 fragment 中使用getActivity()而不是this

替换

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

 }

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

 }

更新

为了避免应用程序崩溃检查:

if (codeSent == null || TextUtils.isEmpty(codeSent)) {
    Toast.makeText(this, "Please wait until code received", Toast.LENGTH_SHORT).show();

    return;
}

PhoneAuthCredential credential = PhoneAuthProvider.getCredential(codeSent, tOTP);

signInWithPhoneAuthCredential(credential);

关于java - 在 Android Fragment 中使用 Firebase 电话身份验证,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62297040/

相关文章:

java - "jmap -histo pid"到底打印了什么

android - Android Gradle-Prolog中不允许内容

java - Google 应用程序引擎中的 Firebase 在初始化时抛出 noMethodFound 错误

java - 如何在 Java 中将控制台数据转换为文本文件

java - 下面的代码中 asynctask 是否正确使用?

java - 生成的 .apk 包含未使用的资源和未使用的类

android - 带类映射器 : No setter/field for "class" found on class in android

ios - 处理来自 continueUserActivity 方法的 Firebase 邀请

Java For循环到递归函数

java - 异常是 : org. gradle.api.tasks.TaskExecutionException:任务执行失败:app:compileDebugJavaWithJavac