使用Android进行Firebase身份验证

时间:2017-01-05 21:03:35

标签: android firebase firebase-authentication progressdialog

我试图让我的Android应用通过Firebase对用户进行身份验证(目前仅使用电子邮件/密码)。我有一个登录,注册和主要活动(主页)。

public class Register extends AppCompatActivity {

private EditText txtEmailAddress, txtPassword;
private FirebaseAuth firebaseAuth;


protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_register);

    txtEmailAddress = (EditText)findViewById(R.id.reg_email);
    txtPassword = (EditText)findViewById(R.id.reg_password);
    firebaseAuth = FirebaseAuth.getInstance();

}

public void btnRegistrationUser_Click(View v) {
    final ProgressDialog progressDialog = ProgressDialog.show(Register.this, "Please wait", "Processing",true);
    firebaseAuth.createUserWithEmailAndPassword(txtEmailAddress.getText().toString(),txtPassword.getText().toString())
            .addOnCompleteListener(new OnCompleteListener<AuthResult>() {
        @Override
        public void onComplete(@NonNull Task<AuthResult> task) {
            progressDialog.dismiss();

            if(task.isSuccessful()){
                Toast.makeText(Register.this,"Registration successful",Toast.LENGTH_LONG).show();
                Intent intent = new Intent(Register.this,LoginActivity.class);
                startActivity(intent);

            }else{
                Log.e("ERROR",task.getException().toString());
                Toast.makeText(Register.this, task.getException().getMessage(),Toast.LENGTH_LONG).show();
            }
            progressDialog.dismiss();
        }
    });
}

这是我的register.java。我可以正常运行应用程序,但在输入信息并单击按钮后,我得到一个无限的ProgressDialog,只是旋转而没有更新到Firebase。

我已经按照不同的教程寻找答案,但似乎找不到任何修复方法。我有我的清单页面允许互联网许可,我不相信我有任何问题与gradle(应用程序运行良好减去不允许我现在到达主页(大声笑),但没有错误),我将google-services.json粘贴在我的应用中。

如果有帮助,下面的代码是我的login.java。感谢您的帮助!

public class LoginActivity extends AppCompatActivity {

private EditText txtEmailLogin;
private EditText txtPassword;

private FirebaseAuth firebaseAuth;
private FirebaseAuth.AuthStateListener mAuthListener;


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_login);

    txtEmailLogin = (EditText) findViewById(R.id.editTextEmail);
    txtPassword = (EditText)findViewById(R.id.editTextPassword);
    firebaseAuth = FirebaseAuth.getInstance();
}

public void btnRegistration_Click(View v){
    Intent registration = new Intent(LoginActivity.this,Register.class);
    startActivity(registration);
}

public void btnUserLogin_Click(View v){
    final ProgressDialog progressDialog = ProgressDialog.show(LoginActivity.this,"Please wait","Processing",true);

    firebaseAuth.signInWithEmailAndPassword(txtEmailLogin.getText().toString(),txtPassword.getText().toString())
            .addOnCompleteListener(new OnCompleteListener<AuthResult>() {
                @Override
                public void onComplete(@NonNull Task<AuthResult> task) {
                    progressDialog.dismiss();

                    if(task.isSuccessful()){
                        Toast.makeText(LoginActivity.this,"Login successful",Toast.LENGTH_LONG).show();
                        Intent intent = new Intent (LoginActivity.this,MainActivity.class);
                        startActivity(intent);

                    }else{
                        Log.e("ERROR",task.getException().toString());
                        Toast.makeText(LoginActivity.this, task.getException().getMessage(),Toast.LENGTH_LONG).show();
                    }
                }
            });
}

1 个答案:

答案 0 :(得分:0)

@Override
public void onStart() {
    super.onStart();
    mAuth.addAuthStateListener(mAuthListener);
}
@Override
public void onStop() {
    super.onStop();
    if (mAuthListener != null) {
        mAuth.removeAuthStateListener(mAuthListener);
    }
}

如果你使用android Studio点击工具和配置很快你的问题似乎是注册onauthstatelistner