我正确地编写了代码,但是无法将otp移至移动设备。我所做的是,从我将手机号码传递到第二个活动后的第一次活动,它的运行就像一种魅力。 onVerificationFailed
中的Toast正在工作(如果我输入了手机号码以外的其他号码),我有sh-1键,我没有将号码保存在Firebase中,我检查了这些内容。
这些是依赖项
dependencies {
classpath 'com.android.tools.build:gradle:3.3.1'
classpath 'com.google.gms:google-services:4.0.0'
这是我验证otp的活动代码
public class VerifyPhoneActivity extends AppCompatActivity {
//These are the objects needed
//It is the verification id that will be sent to the user
private String mVerificationId;
//The edittext to input the code
private EditText editTextCode;
//firebase auth object
private FirebaseAuth mAuth;
String code;
String mobile;
String codesent;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_verify_phone);
mAuth= FirebaseAuth.getInstance();
sendVerificationCode();
findViewById(R.id.button).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
verifySignInCode();
}
});
}
private void verifySignInCode()
{
EditText fd = (EditText) findViewById(R.id.et1);
String value= fd.getText().toString();
// int finalValue=Integer.parseInt(value);
final EditText sd = (EditText) findViewById(R.id.et2);
String value1= sd.getText().toString();
// int finalValue1=Integer.parseInt(value1);
EditText td = (EditText) findViewById(R.id.et3);
String value2= td.getText().toString();
// int finalValue2=Integer.parseInt(value2);
EditText fod = (EditText) findViewById(R.id.et4);
String value3= fod.getText().toString();
//int finalValue3=Integer.parseInt(value3);
EditText fid = (EditText) findViewById(R.id.et5);
String value4= fid.getText().toString();
//int finalValue4=Integer.parseInt(value4);
EditText sid = (EditText) findViewById(R.id.et6);
String value5= sid.getText().toString();
//int finalValue5=Integer.parseInt(value5);
code = value + value1+value2+value3+value4+value5;
PhoneAuthCredential credential = PhoneAuthProvider.getCredential(codesent, code);
signInWithPhoneAuthCredential(credential);
}
private void signInWithPhoneAuthCredential(PhoneAuthCredential credential) {
mAuth.signInWithCredential(credential)
.addOnCompleteListener(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() {
Intent intent = getIntent();
mobile = intent.getStringExtra("mobile");
String phonenumber= "+91"+mobile;
Toast.makeText(getApplicationContext(), phonenumber,
Toast.LENGTH_LONG).show();
PhoneAuthProvider.getInstance().verifyPhoneNumber(
phonenumber, // Phone number to verify
60, // Timeout duration
TimeUnit.SECONDS, // Unit of timeout
this, // Activity (for callback binding)
mCallbacks); // OnVerificationStateChangedCallbacks
}
PhoneAuthProvider.OnVerificationStateChangedCallbacks mCallbacks=new PhoneAuthProvider.OnVerificationStateChangedCallbacks() {
@Override
public void onVerificationCompleted(PhoneAuthCredential phoneAuthCredential) {
}
@Override
public void onVerificationFailed(FirebaseException e) {
Toast.makeText(getApplicationContext(), "faild",
Toast.LENGTH_LONG).show();
}
@Override
public void onCodeSent(String s, PhoneAuthProvider.ForceResendingToken forceResendingToken) {
// super.onCodeSent(s, forceResendingToken);
Toast.makeText(getApplicationContext(), "sent",
Toast.LENGTH_LONG).show();
codesent=s;
}
};
}
我认为问题出在这里。
PhoneAuthProvider.getInstance().verifyPhoneNumber(
phonenumber, // Phone number to verify
60, // Timeout duration
TimeUnit.SECONDS, // Unit of timeout
this, // Activity (for callback binding)
mCallbacks); // OnVerificationStateChangedCallbacks
}
我刚开始使用Android。