所以我一直试图解决这个问题,但我似乎无法知道什么是错的。我有一个按钮,当点击时调用PhoneAuthProvider,它有4个选项:OnVerifiicationCompleted,OnVerificationFailed,OnCodeSent和onCodeAutoRetrieval。问题是正在调用oncodesent,但我放在那里的代码不起作用,甚至调试日志都没有。我得到的只是一条显示验证码的短信。当我将字符串值设置为onCodeSent的verifyID时,字符串的值为:null。这是我的代码:
sendLink.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
popSound.start();
if (greenCheck.getVisibility() == View.VISIBLE) {
//send link
// we will finish this activity and send the link to the number
// an option to resend code to the number: it will be provided at the link
// in this format: resend code to (XXX)-XxX-XXXX
String number = phoneNumber.getText().toString();
phoneNumber.setText("");
sendLink.setEnabled(false);
PhoneAuthProvider.getInstance().verifyPhoneNumber(
number,
60,
TimeUnit.SECONDS,
MobileNumberActivity.this,
new PhoneAuthProvider.OnVerificationStateChangedCallbacks() {
@Override
public void onVerificationCompleted(PhoneAuthCredential phoneAuthCredential) {
signInWithPhoneAuthCredential(phoneAuthCredential);
}
@Override
public void onVerificationFailed(FirebaseException e) {
}
@Override
public void onCodeSent(String s, PhoneAuthProvider.ForceResendingToken forceResendingToken) {
super.onCodeSent(s, forceResendingToken);
// verificationID never gets assigned s and it is null. log.d isn't on logcat
verificationID = s;
Log.d("testing", "onCodeSent: " + verificationID);
}
@Override
public void onCodeAutoRetrievalTimeOut(String s) {
super.onCodeAutoRetrievalTimeOut(s);
}
}
);
Intent i = new Intent(MobileNumberActivity.this, VerificationActivity.class);
startActivity(i);
finish();
} else if (phoneNumber.getText().toString().length() == 0) {
Toast.makeText(MobileNumberActivity.this, "Please enter a phone number", Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(MobileNumberActivity.this, "Please enter a valid phone number", Toast.LENGTH_SHORT).show();
}
}
});
答案 0 :(得分:0)
经过反复试验后终于找到了答案。我不知道为什么这有效,但我的猜测是因为我正在创建一个PhoneAuthProvider的实例,它只适用于oncreate。我在oncreate中有我的,但它被setOnClickListener包围,如上面的代码所示。所以对我有用的是开始将其引导到我的验证活动的意图,在其oncreate方法中,我自己创建了一个PhoneAuthProvider实例并且它有效。
第一项活动:
sendLink.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
popSound.start();
if (greenCheck.getVisibility() == View.VISIBLE) {
//send link
number = phoneNumber.getText().toString();
phoneNumber.setText("");
sendLink.setEnabled(false);
Intent i = new Intent(MobileNumberActivity.this, VerificationActivity.class);
startActivity(i);
finish();
} else if (phoneNumber.getText().toString().length() == 0) {
Toast.makeText(MobileNumberActivity.this, "Please enter a phone number", Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(MobileNumberActivity.this, "Please enter a valid phone number", Toast.LENGTH_SHORT).show();
}
}
});
第二项活动:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_verification);
mAuth = FirebaseAuth.getInstance();
mCallbacks = new PhoneAuthProvider.OnVerificationStateChangedCallbacks() {
@Override
public void onVerificationCompleted(PhoneAuthCredential credential) {
// This callback will be invoked in two situations:
// 1 - Instant verification. In some cases the phone number can be instantly
// verified without needing to send or enter a verification code.
// 2 - Auto-retrieval. On some devices Google Play services can automatically
// detect the incoming verification SMS and perform verification without
// user action.
Log.d("COmpleted", "onVerificationCompleted:" + credential);
signInWithPhoneAuthCredential(credential);
}
@Override
public void onVerificationFailed(FirebaseException e) {
// This callback is invoked in an invalid request for verification is made,
// for instance if the the phone number format is not valid.
Log.w("failed", "onVerificationFailed", e);
if (e instanceof FirebaseAuthInvalidCredentialsException) {
// Invalid request
// ...
} else if (e instanceof FirebaseTooManyRequestsException) {
// The SMS quota for the project has been exceeded
// ...
}
// Show a message and update the UI
// ...
}
@Override
public void onCodeSent(String verificationId,
PhoneAuthProvider.ForceResendingToken token) {
// The SMS verification code has been sent to the provided phone number, we
// now need to ask the user to enter the code and then construct a credential
// by combining the code with a verification ID.
Log.d("codesent", "onCodeSent:" + verificationId);
// Save verification ID and resending token so we can use them later
mVerificationID = verificationId;
mResendToken = token;
// ...
}
};
PhoneAuthProvider.getInstance().verifyPhoneNumber(
MobileNumberActivity.number, // Phone number to verify
60, // Timeout duration
TimeUnit.SECONDS, // Unit of timeout
this, // Activity (for callback binding)
mCallbacks);
}