我编写了一个非常简单的应用程序来测试Firebase的工作情况。我将此依赖项添加到我的build.gradle(App):
implementation 'com.google.firebase:firebase-core:9.6.0'
compile 'com.firebaseui:firebase-ui-auth:4.2.0'
compile 'com.firebaseui:firebase-ui-database:4.2.0'
compile "com.google.firebase:firebase-messaging:17.3.0"
此插件位于文件末尾:
apply plugin: 'com.google.gms.google-services'
和classpath 'com.google.gms:google-services:4.0.1'
到我的build.gradle(project)。
在mainActivity中,我使用firebase身份验证将以下代码添加到注册/登录用户:
public class MainActivity extends AppCompatActivity {
public final int SIGN_IN_REQUEST_CODE=123;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
if(FirebaseAuth.getInstance().getCurrentUser() == null) {
// Start sign in/sign up activity
startActivityForResult(
AuthUI.getInstance()
.createSignInIntentBuilder()
.build(),
SIGN_IN_REQUEST_CODE
);
}
else {
// User is already signed in. Therefore, display
// a welcome Toast
Toast.makeText(this,
"Welcome " + FirebaseAuth.getInstance()
.getCurrentUser()
.getDisplayName(),
Toast.LENGTH_LONG)
.show();
// Load chat room contents
displayChatMessages();
}
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if(requestCode == SIGN_IN_REQUEST_CODE) {
if(resultCode == RESULT_OK) {
Toast.makeText(this,
"Successfully signed in. Welcome!",
Toast.LENGTH_LONG)
.show();
displayChatMessages();
} else {
Toast.makeText(this,
"We couldn't sign you in. Please try again later.",
Toast.LENGTH_LONG)
.show();
// Close the app
finish();
}
}
}
private void displayChatMessages() {
}
}
我调试了if(FirebaseAuth.getInstance().getCurrentUser() == null)
条件执行的代码,但是onActivityResult从不运行。当我在移动设备上进行测试并填写电子邮件,用户名和密码并按保存按钮时,它显示“电子邮件帐户注册失败”错误。我哪里错了?
感谢您的帮助。
答案 0 :(得分:1)
尝试此代码
reg_button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String email = reg_email.getText().toString();
String password = reg_password.getText().toString();
String confirm_pass =confirm_password.getText().toString();
if (!TextUtils.isEmpty(email) && !TextUtils.isEmpty(password) && !TextUtils.isEmpty(confirm_pass))
{
if (password.equals(confirm_pass))
{
reg_progress_bar.setVisibility(View.VISIBLE);
mAuth.createUserWithEmailAndPassword(email,password).addOnCompleteListener(new OnCompleteListener<AuthResult>() {
@Override
public void onComplete(@NonNull Task<AuthResult> task) {
if (task.isSuccessful())
{
Intent settingActivity = new Intent(getApplicationContext(),ProfileActivity.class);
startActivity(settingActivity);
finish();
}
else
{
String errorMessage = task.getException().getMessage();
Toast.makeText(RegisterActivity.this, "error" +errorMessage, Toast.LENGTH_SHORT).show();
}
reg_progress_bar.setVisibility(View.INVISIBLE);
}
});
}
else
{
Toast.makeText(RegisterActivity.this, "password and confirm password does not match", Toast.LENGTH_SHORT).show();
}
}
}
});