如何检查该用户是否已从Facebook注销或令牌已过期

时间:2015-07-06 09:09:00

标签: android facebook android-fragments facebook-sdk-4.0

我有一个片段,我在MainActivity OnCreate方法中调用它。在这个片段中,我要求用户登录脸书。我的代码是这样的:

 public class FbLogin extends Fragment {
        private AccessToken accessToken;
        private Profile profile;
        private ProfileTracker profileTracker;
        static AccessTokenTracker tracker;
        private ProfilePictureView profilepic;
        LoginButton loginButton;
        LoginPrefrence loginPrefrence ;


        private CallbackManager mcallbackManager;

        private FacebookCallback<LoginResult> mcallback = new FacebookCallback<LoginResult>() {
            @Override
            public void onSuccess(LoginResult loginResult) {
                accessToken = loginResult.getAccessToken();
                profile = Profile.getCurrentProfile();
                getFbData(profile);


                GraphRequest request = GraphRequest.newMeRequest(
                        loginResult.getAccessToken(),
                        new GraphRequest.GraphJSONObjectCallback() {
                            @Override
                            public void onCompleted(JSONObject object, GraphResponse graphResponse) {
                                Log.d("Amc", graphResponse.toString());


                                try {
                                    String id = object.getString("id");
                                    String name = object.getString("name");
                                  //  String email = object.getString("email");
                                    String gender = object.getString("gender");
                                    profilepic.setProfileId(object.getString("id"));

                                  //if want to make it round
                                    /*
                                    profileImageView = ((ImageView)faceBookProfilePictureView.getChildAt(0));
                                bitmap  = ((BitmapDrawable)profileImageView.getDrawable()).getBitmap();
                                  circular.setImageBitmap(bitmap);
                                  profilepic.getChildAt(0);
                                     */



                                   // String birthday = object.getString("birthday");
                                  // String Uri1 =(object.getJSONObject("picture").getJSONObject("data").getString("url"));
                                    Log.d("Amc2",""+id + ", " + name + ", " +""+ ""+ ", " + gender + ", " );
                                } catch (JSONException e) {
                                    e.printStackTrace();
                                    Log.d("Amc2",""+e.toString());
                                }



                            }


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

            }


            @Override
            public void onCancel() {

            }

            @Override
            public void onError(FacebookException e) {

            }

        };

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

        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            FacebookSdk.sdkInitialize(getActivity().getApplicationContext());
            mcallbackManager = CallbackManager.Factory.create();
            tracker = new AccessTokenTracker() {
                @Override
                protected void onCurrentAccessTokenChanged(AccessToken oldToken, AccessToken newToken) {

                }
            };
            profileTracker = new ProfileTracker() {
                @Override
                protected void onCurrentProfileChanged(Profile oldProfile, Profile newProfile) {
                    getFbData(newProfile);

                }
            };
            tracker.startTracking();
            profileTracker.startTracking();
        }

        @Override
        public void onStop() {
            super.onStop();
            tracker.stopTracking();
            profileTracker.stopTracking();
        }
        @Override
        public void onPause() {
            super.onPause();
            profileTracker.stopTracking();
            tracker.stopTracking();
        }

        @Override
        public void onDestroy() {
            super.onDestroy();
            tracker.stopTracking();
            profileTracker.stopTracking();
        }

        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container,
                                 Bundle savedInstanceState) {
            // Inflate the layout for this fragment
            return inflater.inflate(R.layout.fragment_fb_login, container, false);
        }

        @Override
        public void onViewCreated(View view, Bundle savedInstanceState) {
            super.onViewCreated(view, savedInstanceState);


            profilepic = (ProfilePictureView)view.findViewById(R.id.pf_view);
            loginPrefrence = new LoginPrefrence(getActivity());
            loginButton = (LoginButton) view.findViewById(R.id.login_button);
            loginButton.setReadPermissions(Arrays.asList("public_profile","email","user_birthday"));
            loginButton.setFragment(this);
            loginButton.registerCallback(mcallbackManager, mcallback);
        }

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

        public void getFbData(Profile prof) {

            if (prof != null) {

                Log.d("Facebook", prof.getFirstName());
                Log.d("Facebook", prof.getName());
                Log.d("Facebook", prof.getId().toString());

            }

        }



    }

这是一个简单的Facebook登录代码。但我想做以下

  1. 如果用户仍在登录,如何签入MianActivity
  2. 如何访问令牌,令牌的主要和实际用途是什么以及如何跟踪它是否已过期
  3. 使用时调用哪个方法将单击我的应用程序中的注销按钮。
  4. 请帮助我,我已经尝试过所有的事情,但没有什么能真正帮助我。

1 个答案:

答案 0 :(得分:0)

使用以下代码行检查当前访问令牌

private Boolean isUserLoggedIn(){
    if (AccessToken.getCurrentAccessToken() != null) {
        //user is logged in already
        return true;
    }else{
        return false;
    }
}