Android facebook强制退出

时间:2016-10-26 12:39:02

标签: java android facebook login

当我退出应用程序时,我不想强​​制从facebook退出,因此每次启动应用程序时都必须登录。如果那是不可能的,我需要帮助检查用户是否已经登录。

 private FacebookCallback<LoginResult> mCallback = new FacebookCallback<LoginResult>() {
        @Override
        public void onSuccess(final LoginResult loginResult) {
            GraphRequest request = GraphRequest.newMeRequest(
                    loginResult.getAccessToken(),
                    new GraphRequest.GraphJSONObjectCallback() {
                        @Override
                        public void onCompleted(
                                JSONObject object,
                                GraphResponse response) {

                            try {
                                if (object != null) {
                                    name  = object.getString("first_name") + " "+object.getString("last_name");
                                    email = object.getString("email");
                                    mTextDetails.setText("Welcome " + name);
                                    editor.putString("username", name);
                                    editor.putString("email", email);
                                    editor.apply();
                                }
                            } catch (JSONException e) {

                            }
                        }
                    });
            Bundle parameters = new Bundle();
            parameters.putString("fields", "id,name,email, first_name, last_name, gender");
            request.setParameters(parameters);
            request.executeAsync();
        }
//        }

        @Override
        public void onCancel() {
        }

        @Override
        public void onError(FacebookException error) {

        }
    };

    public MainFragment() {
        // Required empty public constructor
    }

    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        FacebookSdk.sdkInitialize(getActivity().getApplicationContext());
        mCallbackManager = CallbackManager.Factory.create();
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_main, container, false);
        mTextDetails = (TextView) view.findViewById(R.id.text_details);
        LoginButton loginButton = (LoginButton) view.findViewById(R.id.login_button);
        loginButton.setReadPermissions("public_profile", "email");//Only ask if you must

        loginButton.setFragment(this);
        loginButton.registerCallback(mCallbackManager, mCallback);

        sharedPref = this.getActivity().getSharedPreferences("userinfo", Context.MODE_PRIVATE);
        editor = sharedPref.edit();
        name = sharedPref.getString("username", "");
        email = sharedPref.getString("email","");

        if (name != "") {
            mTextDetails.setText(name);
        }
//        Toast.makeText(getActivity(), sharedPref.getString("username", ""), Toast.LENGTH_LONG).show();
        Toast.makeText(getActivity(), sharedPref.getString("email", ""), Toast.LENGTH_LONG).show();

        return view;
    }

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


    }
}

1 个答案:

答案 0 :(得分:0)

您可以使用此LoginManager.getInstance()。logOut();