我真的是Firebase的新手,它试图在早期构建电话认证页面时工作正常,但是在我创建注册布局并将其链接到该布局之后,它总是无法通过验证。 我已经包括所有必要的依赖项。 注册页面仅收集用户名,电话号码,电子邮件并将其放入Firebase数据库中。
提前谢谢!
有人可以告诉我出了什么问题吗
我的验证布局代码
package kanti.kushal.team_beta;
import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ProgressBar;
import android.widget.Toast;
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.FirebaseUser;
import com.google.firebase.auth.PhoneAuthCredential;
import com.google.firebase.auth.PhoneAuthProvider;
import java.util.concurrent.TimeUnit;
public class verifyphoneActivity extends AppCompatActivity {
private FirebaseAuth mAuth;
private FirebaseUser mcurrentUser;
private String verificationid = null;
Button btn_verifyotp;
EditText txt_code;
ProgressBar progress_Bar;
String phone;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_verifyphone);
txt_code =findViewById(R.id.verifycode);
//sent verification code
phone = getIntent().getStringExtra("phoneme");
send_VerificationCode(phone);
mAuth = FirebaseAuth.getInstance();
btn_verifyotp = findViewById(R.id.verifyotp);
mcurrentUser = mAuth.getCurrentUser();
progress_Bar =findViewById(R.id.progress_bar);
btn_verifyotp.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String code =txt_code.getText().toString();
if(code.length()==6) {
verifycode(code);
}
else{
Toast.makeText(verifyphoneActivity.this, "Enter correct OTP", Toast.LENGTH_SHORT).show();
}
}
});
}
private void verifycode(String code_txt){
PhoneAuthCredential credential =PhoneAuthProvider.getCredential(verificationid,code_txt);
signInwithCredentail(credential);
}
private void signInwithCredentail(PhoneAuthCredential credential) {
mAuth.signInWithCredential(credential)
.addOnCompleteListener(new OnCompleteListener<AuthResult>() {
@Override
public void onComplete(@NonNull Task<AuthResult> task) {
if(task.isSuccessful()){
Intent intent = new Intent(verifyphoneActivity.this,RegisterActivity.class);
intent.putExtra("ph_no",phone);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK |Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);
}
else{
Toast.makeText(verifyphoneActivity.this,task.getException().getMessage(), Toast.LENGTH_SHORT).show();
}
}
});
}
private void send_VerificationCode(String ph_no){
PhoneAuthProvider.getInstance()
.verifyPhoneNumber(
ph_no,
60,
TimeUnit.SECONDS,
this,
mCallback);
}
private PhoneAuthProvider.OnVerificationStateChangedCallbacks mCallback = new PhoneAuthProvider.OnVerificationStateChangedCallbacks() {
@Override
public void onCodeSent(@NonNull String s, @NonNull PhoneAuthProvider.ForceResendingToken forceResendingToken) {
super.onCodeSent(s, forceResendingToken);
verificationid = s;
}
@Override
public void onVerificationCompleted(@NonNull PhoneAuthCredential phoneAuthCredential) {
String code = phoneAuthCredential.getSmsCode();
if(code!=null){
txt_code.setText(code);
verifycode(code);
}
}
@Override
public void onVerificationFailed(@NonNull FirebaseException e) {
Toast.makeText(verifyphoneActivity.this, "Verification Failed. Try Again...", Toast.LENGTH_SHORT).show();
}
};
@Override
public void onStart() {
super.onStart();
// Check if user is signed in (non-null) and update UI accordingly.
if(mcurrentUser != null)
{
Intent intent = new Intent(verifyphoneActivity.this,MainActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK |Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);
finish();
}
}
}