其他分享
首页 > 其他分享> > Android FacebookSDK V4.20使用LoginManager登录和注销

Android FacebookSDK V4.20使用LoginManager登录和注销

作者:互联网

花了几天时间试图弄清楚这一点之后,我无法提出任何可行的方法.使用我的代码,它向我显示了我接受并登录的权限表,但没有进入onSuccess,onError或onCancel.每当我单击按钮时,它什么都不做.而且LogCat上没有错误.我不知道我要去哪里错了.

fb = (Button) findViewById(R.id.fb_button);
    fb.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {
           LoginManager.getInstance().logInWithReadPermissions(LoginActivity.this,permissionNeeds);
            callbackManager = CallbackManager.Factory.create();

            LoginManager.getInstance().registerCallback(callbackManager,new FacebookCallback<LoginResult>() {
                @Override
                public void onSuccess(LoginResult loginResult) {
                    Log.d("kkkkkk","kkllkl");
                }

                @Override
                public void onCancel() {
                    Log.d("kkkkkk","kkllkl2");
                }

                @Override
                public void one rror(FacebookException error) {
                    Log.d("kkkkkk","kkllkl4");
                }
            });


        }
    });

FacebookSdk.sdkInitialize(this.getApplicationContext());在super.onCreate(savedInstanceState);之后初始化

解决方法:

我终于弄明白了.我将代码从初始代码更改为:

 @Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    FacebookSdk.sdkInitialize(getApplicationContext());
    callbackManager = CallbackManager.Factory.create();
    LoginManager.getInstance().registerCallback(callbackManager,new FacebookCallback<LoginResult>() {
        @Override
        public void onSuccess(LoginResult loginResult) {
            Log.d("kkkkkk","kkllkl");
        }

        @Override
        public void onCancel() {
            Log.d("kkkkkk","kkllkl2");
        }

        @Override
        public void one rror(FacebookException error) {
            Log.d("kkkkkk","kkllkl4");
        }
    });

然后在按钮的点击监听器上,我只需要拨打登录电话即可.

 fb.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {
            LoginManager.getInstance().logInWithReadPermissions(LoginActivity.this,permissionNeeds);

        }
    });

也不要忘记添加onActivityResult.我也犯了错误.

   @Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    callbackManager.onActivityResult(requestCode, resultCode, data);
}

标签:facebook,facebook-login,facebook-android-sdk,android
来源: https://codeday.me/bug/20191028/1950678.html