简体   繁体   English

无法使用Facebook SDK登录

[英]Unable to login using Facebook SDK

In my app, I am trying to implement Facebook login. 在我的应用中,我正在尝试实现Facebook登录。 I have an IntroLogin Activity after splash to determine whether the user is already logged in or not. 启动后我有一个IntroLogin活动,以确定用户是否已经登录。 If the user is already logged in, it straight loads MainActivity. 如果用户已经登录,它将直接加载MainActivity。 Otherwise, it asks the user to log in. 否则,它要求用户登录。

My method to determine if the user is logged in or not works well in Android 6+. 我确定用户是否已登录的方法在Android 6+中运行良好。 However, in Android Lollipop, after user sign-in (user get's to give app authenticate to use there info in popup), the app closes. 但是,在Android Lollipop中,在用户登录后(用户获得使应用程序进行身份验证以在弹出窗口中使用那里的信息),该应用程序将关闭。 I get no FCs or any messages in log. 我在日志中没有FC或任何消息。 It just closes. 它刚刚关闭。 Even if the user restarts the app, it again prompts to login. 即使用户重新启动应用程序,它也会再次提示登录。

My IntroLogin Activity: 我的IntroLogin活动:

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

    sharedPreferences = PreferenceManager.getDefaultSharedPreferences(this);

    loginButton = (Button) findViewById(R.id.login_button);

    FacebookSdk.sdkInitialize(getApplicationContext());
    callbackManager = CallbackManager.Factory.create();

    AccessToken accessToken = AccessToken.getCurrentAccessToken();
    if(accessToken != null){
        finish();
    }
    else{
        loginButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                PopupLogin();
            }
        });
    }
}

public void PopupLogin() {
    // Set permissions
    LoginManager.getInstance().logInWithReadPermissions(this, Arrays.asList("public_profile"));
    LoginManager.getInstance().setLoginBehavior(LoginBehavior.WEB_VIEW_ONLY).registerCallback(callbackManager, new FacebookCallback<LoginResult>() {
        @Override
        public void onSuccess(LoginResult loginResult) {
            Log.i(TAG, "onSuccess: " + loginResult);
            startActivity(new Intent(IntroLogin.this, MainActivity.class));
            finish();
        }

        @Override
        public void onCancel() {
            Log.d("MyApp", "Login canceled");
            Snackbar snackbar = Snackbar.make(loginButton, "Login process canceled", Snackbar.LENGTH_INDEFINITE);
            snackbar.getView().setBackgroundColor(ContextCompat.getColor(IntroLogin.this, R.color.colorPrimaryDark));
            snackbar.setActionTextColor(ContextCompat.getColor(IntroLogin.this, R.color.pure_white));
            snackbar.setAction(R.string.retry_login, new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    loginButton.performClick();
                }
            });
            snackbar.show();
        }

        @Override
        public void onError(FacebookException error) {
            Log.d("Myapp", error.toString());
            Toast.makeText(IntroLogin.this, error.getMessage(), Toast.LENGTH_LONG).show();
        }
    });
}

UPDATE: 更新:

I found the cause but not the solution, I think OnActivityResult is not getting called in lollipop, that maybe the reason i am not seeing any log. 我找到了原因,但没有找到解决方案,我认为在棒棒糖中未调用OnActivityResult ,这也许是我没有看到任何日志的原因。

you should initialize your facebooksdk just after calling super.oncreate() 您应该在调用super.oncreate()之后立即初始化facebooksdk

And also you are calling finish() when accessToken != null which means when user is already logged in facebook it will close the activity.. 而且,当accessToken != null时,您正在调用finish() ,这意味着当用户已经登录Facebook时,它将关闭活动。

Change your onCreate() method code to: 将您的onCreate()方法代码更改为:

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        FacebookSdk.sdkInitialize(getApplicationContext());
        setContentView(R.layout.activity_intro_login);
        callbackManager = CallbackManager.Factory.create();
        sharedPreferences = PreferenceManager.getDefaultSharedPreferences(this);

         loginButton = (Button) findViewById(R.id.login_button);
         updateWithToken(AccessToken.getCurrentAccessToken());
        accessTokenTracker = new AccessTokenTracker() {
            @Override
            protected void onCurrentAccessTokenChanged(AccessToken oldToken, AccessToken newToken) {
                updateWithToken(newToken);
            }
        };
        accessTokenTracker.startTracking();
    }

private void updateWithToken(AccessToken currentAccessToken) {

        if (currentAccessToken != null) {
            Log.d("FB", "User Logged IN.");
            //programetically logout user or do your on success
            LoginManager.getInstance().logOut();
        } else {
            Log.d("FB", "User Logged Out.");
            loginButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                PopupLogin();
            }
        });
      }
  }

Found out the cause. 找出原因。 I have added android:launchMode="singleTop" in my loginActivity, that was the reason why OnActivityResult was not calling. 我在我的loginActivity中添加了android:launchMode="singleTop" ,这就是OnActivityResult未调用的原因。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM