gpt4 book ai didi

android - phoneauthprovider 的 oncodesent 不工作

转载 作者:行者123 更新时间:2023-11-29 14:12:28 24 4
gpt4 key购买 nike

所以我一直在尝试解决这个问题,但我似乎不知道哪里出了问题。我有一个按钮,单击它时会调用 PhoneAuthProvider,它有 4 个选项:OnVerifiicationCompleted、OnVerificationFailed、OnCodeSent 和 onCodeAutoRetrieval。问题是正在调用 oncodesent 但我放入其中的代码不起作用,甚至没有调试日志。我得到的只是一条显示验证码的短信。而当我给onCodeSent的verificationID设置一个字符串值时,该字符串为value: 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();
}
}
});

最佳答案

经过反复试验终于找到了答案。我不太清楚为什么会这样,但我的猜测是因为我正在创建 PhoneAuthProvider 的实例,所以它只在 oncreate 中有效。我在 oncreate 中有我的,但它被 setOnClickListener 包围,如您在上面的代码中所见。因此,对我有用的是开始将其定向到我的验证 Activity 的 Intent ,在它的 oncreate 方法中,我单独创建了一个 PhoneAuthProvider 实例并且它起作用了。

第一个 Activity :

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();
}
}
});

第二个 Activity :

@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);
}

关于android - phoneauthprovider 的 oncodesent 不工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49575601/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com