Java 在android内部使用Firebase电话认证
我正在制作一个android应用程序,其中唯一的用户登录和注册方法是通过电话认证。我有一个单一的活动,并利用片段 我想在SignInFragment中添加firebase phone身份验证,并使用firebase PhoneAuthProvider。但是,我认为这仅适用于活动,因为我尝试在片段中实现,但我的应用程序崩溃了 这是SignInFragment.java的代码Java 在android内部使用Firebase电话认证,java,android,firebase,android-fragments,firebase-realtime-database,Java,Android,Firebase,Android Fragments,Firebase Realtime Database,我正在制作一个android应用程序,其中唯一的用户登录和注册方法是通过电话认证。我有一个单一的活动,并利用片段 我想在SignInFragment中添加firebase phone身份验证,并使用firebase PhoneAuthProvider。但是,我认为这仅适用于活动,因为我尝试在片段中实现,但我的应用程序崩溃了 这是SignInFragment.java的代码 package com.example.XX; import android.os.Bundle; import and
package com.example.XX;
import android.os.Bundle;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
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.FirebaseAuthInvalidCredentialsException;
import com.google.firebase.auth.FirebaseUser;
import com.google.firebase.auth.PhoneAuthCredential;
import com.google.firebase.auth.PhoneAuthProvider;
import java.util.concurrent.Executor;
import java.util.concurrent.TimeUnit;
public class SignInFragment extends Fragment {
private static final String TAG ="INSIDE_SIGN_IN_FRAGMENT" ;
FirebaseAuth mAuth;
EditText xPhone;
EditText xOTP;
String codeSent;
public SignInFragment() {
// Required empty public constructor
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
return inflater.inflate(R.layout.fragment_sign_in, container, false);
}
@Override
public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
Button getOTP=view.findViewById(R.id.button_xOTP);
Button submitButton=view.findViewById(R.id.signInbutton);
xPhone=view.findViewById(R.id.editText_xMobile);
xOTP=view.findViewById(R.id.editText_xOTP);
mAuth=FirebaseAuth.getInstance();
getOTP.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
sendVerificationCode();
}
});
submitButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
verifyOTP();
}
});
}
private void verifyOTP(){
String tOTP=xOTP.getText().toString().trim();
if(tOTP.isEmpty()){
xOTP.setError("OTP is required!");
xOTP.requestFocus();
return;
}
if(tOTP.length()<6){
xOTP.setError("Please enter a valid OTP!");
xOTP.requestFocus();
return;
}
PhoneAuthCredential credential = PhoneAuthProvider.getCredential(codeSent, tOTP);
signInWithPhoneAuthCredential(credential);
}
private void signInWithPhoneAuthCredential(PhoneAuthCredential credential) {
mAuth.signInWithCredential(credential)
.addOnCompleteListener((Executor) 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() {
String xMobileNumber=xPhone.getText().toString().trim();
if(xMobileNumber.isEmpty()){
xPhone.setError("Phone number is required!");
xPhone.requestFocus();
return;
}
if(xMobileNumber.length()<10){
xPhone.setError("Please enter a valid phone number!");
xPhone.requestFocus();
return;
}
PhoneAuthProvider.getInstance().verifyPhoneNumber(
xMobileNumber, // Phone number to verify
60, // Timeout duration
TimeUnit.SECONDS, // Unit of timeout
(Executor) this, // Activity (for callback binding) //NOT SURE ABOUT THIS
mCallbacks); // OnVerificationStateChangedCallbacks
}
PhoneAuthProvider.OnVerificationStateChangedCallbacks mCallbacks=new PhoneAuthProvider.OnVerificationStateChangedCallbacks() {
@Override
public void onVerificationCompleted(@NonNull PhoneAuthCredential phoneAuthCredential) {
}
@Override
public void onVerificationFailed(@NonNull FirebaseException e) {
}
@Override
public void onCodeSent(@NonNull String s, @NonNull PhoneAuthProvider.ForceResendingToken forceResendingToken) {
super.onCodeSent(s, forceResendingToken);
codeSent=s;
}
};
}
我知道问题出在执行器部分,IDE建议在两个地方使用执行器。可能PhoneAuth方法不适用于片段。
任何人都可以回顾这一点,或者告诉我们一种仅在片段中实现Firebase电话身份验证的替代方法。
此活动中使用的。在片段中使用getActivity
而不是this
替换
PhoneAuthProvider.getInstance().verifyPhoneNumber(
xMobileNumber, // Phone number to verify
60, // Timeout duration
TimeUnit.SECONDS, // Unit of timeout
(Executor) this, // Activity (for callback binding) //NOT SURE ABOUT THIS
mCallbacks); // OnVerificationStateChangedCallbacks
}
与
更新
要避免应用程序崩溃检查,请执行以下操作:
if (TextUtils.isEmpty(codeSent) || codeSent == null) {
Toast.makeText(this, "Please wait until code received", Toast.LENGTH_SHORT).show();
return;
}
PhoneAuthCredential credential = PhoneAuthProvider.getCredential(codeSent, tOTP);
signInWithPhoneAuthCredential(credential);
我用getActivity()替换了这两个(执行器)。我没有收到任何编译错误,但我的设备也没有收到OTP。如果我没有输入手机号码,只需输入任意OTP并单击提交,我的应用程序就会崩溃。要获得OTP,您需要提供带有国家代码和加号(+1239 2390)的电话号码
PhoneAuthProvider.getInstance().verifyPhoneNumber(
xMobileNumber, // Phone number to verify
60, // Timeout duration
TimeUnit.SECONDS, // Unit of timeout
getActivity(), // Activity (for callback binding) //NOT SURE ABOUT THIS
mCallbacks); // OnVerificationStateChangedCallbacks
}
if (TextUtils.isEmpty(codeSent) || codeSent == null) {
Toast.makeText(this, "Please wait until code received", Toast.LENGTH_SHORT).show();
return;
}
PhoneAuthCredential credential = PhoneAuthProvider.getCredential(codeSent, tOTP);
signInWithPhoneAuthCredential(credential);