Android Studio 中的 Firebase 电话 OTP 验证

问题描述

我正在尝试使用 Firebase 电话号码身份验证来验证电话号码。我有3个活动。第一个是“EnterMobile”,第二个是“OtpVerification”,最后一个是“仪表板”。我可以发送 OTP,但是当我输入 OTP 并单击验证按钮时,它会返回到第一个活动而不是仪表板活动。

public class OtpVerification extends AppCompatActivity {
    private EditText inputotp1,inputotp2,inputotp3,inputotp4,inputotp5,inputotp6;
    private String getotpbackend;
    public OtpVerification() {
        super();
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_otp_verification);
        inputotp1 = findViewById(R.id.inputotp1);
        inputotp2 = findViewById(R.id.inputotp2);
        inputotp3 = findViewById(R.id.inputotp3);
        inputotp4 = findViewById(R.id.inputotp4);
        inputotp5 = findViewById(R.id.inputotp5);
        inputotp6 = findViewById(R.id.inputotp6);
        getotpbackend = getIntent().getStringExtra("backendOtp");
        final ProgressBar progressBarSendOtp = findViewById(R.id.sending_otp_progress_bar);
        final Button submitOtp = findViewById(R.id.submitOtp);
        TextView ShowMobile = findViewById(R.id.ShowMobile);
        Intent intent = getIntent();
        ShowMobile.setText(String.format(
                "+971-%s",intent.getStringExtra("mobile")
        ));



        submitOtp.setonClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (!inputotp1.getText().toString().trim().isEmpty() && !inputotp2.getText().toString().trim().isEmpty() && !inputotp3.getText().toString().trim().isEmpty() && !inputotp4.getText().toString().trim().isEmpty() && !inputotp5.getText().toString().trim().isEmpty() && !inputotp6.getText().toString().trim().isEmpty()) {
                    String entercodeotp = inputotp1.getText().toString() +
                            inputotp2.getText().toString() +
                            inputotp3.getText().toString() +
                            inputotp4.getText().toString() +
                            inputotp5.getText().toString() +
                            inputotp6.getText().toString();
                    if (getotpbackend != null) {
                        progressBarSendOtp.setVisibility(View.VISIBLE);
                        submitOtp.setVisibility(View.INVISIBLE);
                        PhoneAuthCredential phoneAuthCredential = PhoneAuthProvider.getCredential(
                                getotpbackend,entercodeotp
                        );
                        FirebaseAuth.getInstance().signInWithCredential(phoneAuthCredential)
                                .addOnCompleteListener(new OnCompleteListener<AuthResult>() {
                                    @Override
                                    public void onComplete(@NonNull  Task<AuthResult> task) {
                                        progressBarSendOtp.setVisibility(View.GONE);
                                        submitOtp.setVisibility(View.VISIBLE);
                                        if (task.isSuccessful()) {
                                            Intent intent = new Intent(getApplicationContext(),dashboard.class);
                                            intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
                                            startActivity(intent);
                                        } else {
                                            Toast.makeText(OtpVerification.this,"Enter Correct OTP",Toast.LENGTH_SHORT).show();
                                        }
                                    }
                                });
                    } else {
                        Toast.makeText(OtpVerification.this,"Please Check Internet Connection",Toast.LENGTH_SHORT).show();
                    }
                    //   Toast.makeText(OtpVerification.this,"otp verify",Toast.LENGTH_SHORT).show();
                } else {
                    Toast.makeText(OtpVerification.this,"Please enter all OTP number",Toast.LENGTH_SHORT).show();
                }
            }
        });
        numberOtpMove();
        TextView ResendLabel = findViewById(R.id.SendAgain);
        ResendLabel.setonClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                PhoneAuthProvider.getInstance().verifyPhoneNumber(
                        "+971" + getIntent().getStringExtra("mobile"),60,TimeUnit.SECONDS,OtpVerification.this,new PhoneAuthProvider.OnVerificationStateChangedCallbacks() {
                            @Override
                            public void onVerificationCompleted(@NonNull  PhoneAuthCredential phoneAuthCredential) {
                                Intent intent = new Intent(getApplicationContext(),dashboard.class);
                                intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
                                startActivity(intent);
                                finish();
                            }

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

                                Toast.makeText(OtpVerification.this,e.getMessage(),Toast.LENGTH_SHORT).show();
                            }

                            @Override
                            public void onCodeSent(@NonNull @NotNull String newbackendotp,@NonNull PhoneAuthProvider.ForceResendingToken forceResendingToken) {
                                super.onCodeSent(newbackendotp,forceResendingToken);
                                getotpbackend = newbackendotp;
                                Toast.makeText(OtpVerification.this,"OTP Sent Again",Toast.LENGTH_SHORT).show();
                            }
                        }
                );
            }
        });
    }

    private void numberOtpMove() {

        inputotp1.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s,int start,int count,int after) {

            }

            @Override
            public void onTextChanged(CharSequence s,int before,int count) {
                if (!s.toString().trim().isEmpty()) {
                    inputotp2.requestFocus();

                }
            }

            @Override
            public void afterTextChanged(Editable s) {

            }
        });
        inputotp2.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s,int count) {
                if (!s.toString().trim().isEmpty()) {
                    inputotp3.requestFocus();

                }
            }

            @Override
            public void afterTextChanged(Editable s) {

            }
        });
        inputotp3.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s,int count) {
                if (!s.toString().trim().isEmpty()) {
                    inputotp4.requestFocus();

                }
            }

            @Override
            public void afterTextChanged(Editable s) {

            }
        });
        inputotp4.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s,int count) {
                if (!s.toString().trim().isEmpty()) {
                    inputotp5.requestFocus();

                }
            }

            @Override
            public void afterTextChanged(Editable s) {

            }
        });
        inputotp5.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s,int count) {
                if (!s.toString().trim().isEmpty()) {
                    inputotp6.requestFocus();

                }
            }

            @Override
            public void afterTextChanged(Editable s) {

            }
        });

    }
}

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)