Facebook登录Android应用程序后如何启动MainActivity?

时间:2017-06-06 05:44:06

标签: android facebook firebase firebase-authentication facebook-login

我正在尝试通过Firebase将Facebook登录集成到我的应用中。当用户点击"继续使用Facebook"按钮,我看到一个蓝色的进度条,然后按钮上的文字变为" logout"。我打算在登录成功时打开另一个Activity。我已经使用了startActivity()方法。然而,没有任何反应。请帮忙!

以下是我的登录活动的onCreate()方法的摘录

     callbackManager = CallbackManager.Factory.create();
    // Register your callback
    LoginManager.getInstance().registerCallback(callbackManager,
            // If the login attempt is successful, then call onSuccess and pass the LoginResult
            new FacebookCallback<LoginResult>() {
                @Override
                public void onSuccess(LoginResult loginResult) {

                    // The HomeScreen.class should load, it is not happening.
                    startActivity(new Intent(getApplicationContext() ,HomeScreen.class));
                    // I get this Log on my screen, which is proof that this loop is executed
                    Log.w("TAG", "User ID: " +
                            loginResult.getAccessToken().getUserId() + "\n" +
                            "Auth Token: " + loginResult.getAccessToken().getToken());
                }
                // If the user cancels the login, then call onCancel
                @Override
                public void onCancel() {
                    Toast.makeText(getApplicationContext(),
                            "Canceled login through Facebook",Toast.LENGTH_LONG).show();
                }
                // If an error occurs, then call onError
                @Override
                public void onError(FacebookException exception) {
                    Toast.makeText(getApplicationContext(),
                            " Facebook Login failed!",Toast.LENGTH_LONG).show();
                    Log.w("FB failed", exception);
                }
            });

我允许通过Firebase登录Google,为此我编写了onActivityResult方法。就是这个 -

    public void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);

    // Result returned from launching the Intent from GoogleSignInApi.getSignInIntent(...);
    if (requestCode == RC_SIGN_IN) {
        GoogleSignInResult result = Auth.GoogleSignInApi.getSignInResultFromIntent(data);
        if (result.isSuccess()) {
            // Google Sign In was successful, authenticate with Firebase
            GoogleSignInAccount account = result.getSignInAccount();
            firebaseAuthWithGoogle(account);

        } else {
            // Handle failed signin here
           Toast.makeText(getApplicationContext(),
                   "Google Sign In Failed, Please try with Facebook or Email!", Toast.LENGTH_LONG).show();
        }
    }
    callbackManager.onActivityResult(requestCode, resultCode, data);

}

3 个答案:

答案 0 :(得分:0)

试试这个:

{"items":[{"reputation":6623,"user_id":836,"location":"Durham, United Kingdom","link":"https://datascience.stackexchange.com/users/836/neil-slater","display_name":"Neil Slater"},{"reputation":6267,"user_id":381,"location":"Silicon Valley","link":"https://datascience.stackexchange.com/users/381/emre","display_name":"Emre"},{"reputation":5033,"user_id":2452,"location":"Atlanta, GA","link":"https://datascience.stackexchange.com/users/2452/aleksandr-blekh","display_name":"Aleksandr Blekh"},{"reputation":3537,"user_id":11097,"location":"Gurgaon, India","link":"https://datascience.stackexchange.com/users/11097/dawny33","display_name":"Dawny33"},{"reputation":3257,"user_id":21,"location":"London, United Kingdom","link":"https://datascience.stackexchange.com/users/21/sean-owen","display_name":"Sean Owen"}]

答案 1 :(得分:0)

如果您使用片段而不是将此添加到您的fb登录按钮

buttonLogin.setFragment(this);

并在你的onSuccess()方法中,试试这个

startActivity(new Intent(getActivity() ,HomeScreen.class));

答案 2 :(得分:0)

尝试使用Facebook登录时尝试使用权限。例如:

LoginManager.getInstance().logInWithReadPermissions(getActivity(), Arrays.asList("public_profile", "email", "user_birthday", "user_photos"));