为新的Firebase

时间:2016-05-27 21:42:22

标签: android login firebase

我试图在2016年5月更新后尝试在新的Firebase中更新我的Android代码,但遇到了问题。以前我的用户创建

工作正常
        Firebase ref = new Firebase("https://project.firebaseio.com");
        ref.createUser(email, password, new Firebase.ValueResultHandler<Map<String, Object>>() {
            @Override
            public void onSuccess(Map<String, Object> result) {
                System.out.println("Successfully created user account with uid: " + result.get("uid"));
                error.setText("Account successfully created.");
            }
            @Override
            public void onError(FirebaseError firebaseError) {
                error.setText("Error with account creation");
            }
        });

但在新系统中,我告诉我需要在此处实施系统:https://firebase.google.com/docs/auth/android/password-auth,我收到错误

Cannot resolve method AddOnCompleteListener

每当我尝试将方法放在Android clickListener中时(我如何发送登录数据)

我的(相关)代码是

    FirebaseAuth mAuth = FirebaseAuth.getInstance();
    FirebaseAuth.AuthStateListener mAuthListener;


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


            mAuthListener = new FirebaseAuth.AuthStateListener() {
                @Override
                public void onAuthStateChanged(@NonNull FirebaseAuth firebaseAuth) {
                    FirebaseUser user = firebaseAuth.getCurrentUser();
                    if (user != null) {
                        // User is signed in
                        //Log.d(TAG, "onAuthStateChanged:signed_in:" + user.getUid());
                    } else {
                        // User is signed out
                       // Log.d(TAG, "onAuthStateChanged:signed_out");
                    }
                    // ...
                }
            };

    @Override
    protected void onStart() {
        super.onStart();

    mAuth.addAuthStateListener(mAuthListener);

        mCreateNew.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
   EditText editText = (EditText) findViewById(R.id.email);
                String email = editText.getText().toString();
                editText = (EditText) findViewById(R.id.password);
                String password = editText.getText().toString();



             mAuth.createUserWithEmailAndPassword(email, password).addOnCompleteListener(this, new OnCompleteListener<AuthResult>() {
                @Override
                public void onComplete(@NonNull Task<AuthResult> task) {
                    Log.d(logStr, "createUserWithEmail:onComplete:" + task.isSuccessful());

                    // If sign in fails, display a message to the user. If sign in succeeds
                    // the auth state listener will be notified and logic to handle the
                    // signed in user can be handled in the listener.
                    if (!task.isSuccessful()) {
                        Toast.makeText(getApplicationContext(), "Authentication failed.",
                                Toast.LENGTH_SHORT).show();
                    }

                }
            });
        }
    });
}


mLogin.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                EditText editText = (EditText) findViewById(R.id.email);
                String email = editText.getText().toString();
                editText = (EditText) findViewById(R.id.password);
                String password = editText.getText().toString();

mAuth.signInWithEmailAndPassword(email, password)
                        .addOnCompleteListener(this, new OnCompleteListener<AuthResult>() {
                            @Override
                            public void onComplete(@NonNull Task<AuthResult> task) {
                                Log.d(logStr, "signInWithEmail:onComplete:" + task.isSuccessful());

                                // If sign in fails, display a message to the user. If sign in succeeds
                                // the auth state listener will be notified and logic to handle the
                                // signed in user can be handled in the listener.
                                if (!task.isSuccessful()) {
                                    Log.w(logStr, "signInWithEmail", task.getException());
                                    Toast.makeText(getApplicationContext(), "Authentication failed.",
                                            Toast.LENGTH_SHORT).show();
                                }

                            }
                        });



            }
        });

通过新的Firebase指南,它提到这些新的听众等待用户的更新&#34;登录状态&#34;但是并没有真正详细说明。我如何制作它,以便只有在单击按钮时才能调用登录/创建新内容?

我知道将代码移到clicklistener之外&#34;解决&#34;问题,但后来我不知道如何控制用户何时发送登录数据。

1 个答案:

答案 0 :(得分:4)

我发现了一个回答我的Firebase 9.0.0 mAuth.signInWithEmailAndPassword, how to pass it to a button

的类似问题

似乎这个问题很常见,因此需要对Firebase网站进行更多说明。基本上,.addOnCompleteListener()需要在登录活动中声明为它自己的类。

mAuth.signInWithEmailAndPassword(email, password)
                .addOnCompleteListener(LoginActivity.this, new OnCompleteListener<AuthResult>() {