如何从另一项活动登出facebook?

时间:2015-08-01 06:02:17

标签: android facebook sdk

我正在使用facebook SDK 4.4.0。我想从另一个活动登出facebook吗? 如何在其他活动中添加注销功能? 我想在成功登录后转到另一个活动,并在该活动中添加注销功能

loginButton.registerCallback(callbackManager,
                new FacebookCallback<LoginResult>() {
                    @Override
                    public void onSuccess(LoginResult loginResult) {
                        new fblogin().execute(loginResult.getAccessToken());

                    }

                    @Override
                    public void onCancel() {

                    }

                    @Override
                    public void onError(FacebookException e) {

                    }
                });

    }

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

    class fblogin extends AsyncTask<AccessToken, String, String> {

        @Override
        protected void onPreExecute() {
            super.onPreExecute();
            pDialog = new ProgressDialog(MainActivity.this);
            pDialog.setMessage("Wait.....");
            pDialog.setIndeterminate(false);
            pDialog.setCancelable(false);
            pDialog.show();
        }

        protected String doInBackground(AccessToken... params) {
            GraphRequest request = GraphRequest.newMeRequest(params[0],
                    new GraphRequest.GraphJSONObjectCallback() {
                        @Override
                        public void onCompleted(JSONObject object,
                                GraphResponse response) {

                            Log.v("MainActivity", response.toString());

                            try {

                                username = object.getString("first_name");
                                emailid = object.getString("email");
                                gender = object.getString("gender");

                                Log.v("User Name", username);
                                Log.v("Email", emailid);
                                Log.v("Gender", gender);

                            } catch (JSONException e) {
                                // TODO Auto-generated catch
                                // block
                                e.printStackTrace();
                            }
                        }

                    });
            Bundle parameters = new Bundle();
            parameters.putString("fields",
                    "id,first_name,email,gender,birthday");
            request.setParameters(parameters);
            request.executeAndWait();

            return null;
        }

        protected void onPostExecute(String file_url) {

            pDialog.dismiss();

        }

    }

3 个答案:

答案 0 :(得分:7)

首先初始化您活动的FacebookSdk.sdkInitialize(getApplicationContext());,然后在按钮点击事件中编写以下代码:

LoginManager.getInstance().logOut();

完成!!!

答案 1 :(得分:0)

试试这个方法

public static void LogoutFB(Context context) {
Session session = Session.getActiveSession();
if (session != null) {

    if (!session.isClosed()) {
        session.closeAndClearTokenInformation();
        //clear your preferences if saved
    }
} else {

    session = new Session(context);
    Session.setActiveSession(session);

    session.closeAndClearTokenInformation();
        //clear your preferences if saved
}

}

答案 2 :(得分:0)

确保在SignIN之后隐藏Facebook登录按钮的可见性。

然后在普通Button上使用setOnClickListner并使用以下命令:

LoginManager.getInstance().logOut();

和Toast确认用户。