How to handle Facebook Android SDK v4.1 Access Token between Activities

别说谁变了你拦得住时间么 提交于 2019-12-12 15:14:35

问题


When i go to another activity after successfully logged a user in i can't access to the Facebook Access token

AccessToken.getCurrentAccessToken()

I got this in the LogCat:

{AccessToken token:ACCESS_TOKEN_REMOVED permissions:[]}

回答1:


My solution if you are using the android login button is: To use a fragment and call it inside every activity, you can choose to display it or not depending on what the current activity will be doing, all the fragment's methods will work just fine.

Here is some code:

package com.infoplusplus.anix.currentapp;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.facebook.AccessToken;
import com.facebook.AccessTokenTracker;
import com.facebook.CallbackManager;
import com.facebook.FacebookCallback;
import com.facebook.FacebookException;
import com.facebook.FacebookSdk;
import com.facebook.Profile;
import com.facebook.ProfileTracker;
import com.facebook.login.LoginResult;
import com.facebook.login.widget.LoginButton;


/**
 * Created by Anix on 09/04/2015.
 */
public class LoginFacebookFragment extends Fragment {
    public LoginFacebookFragment() {
    }

    private ProfileTracker profileTracker;
    private AccessTokenTracker tokenTracker;
    private LoginButton loginButton;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        FacebookSdk.sdkInitialize(getActivity().getApplicationContext());
        mCallbackManager = CallbackManager.Factory.create();
        tokenTracker = new AccessTokenTracker() {
            @Override
            protected void onCurrentAccessTokenChanged(AccessToken oldToken, AccessToken newToken) {
            }
        };
        profileTracker = new ProfileTracker() {
            @Override
            protected void onCurrentProfileChanged(Profile oldProfile, Profile newProfile) {
                if (newProfile != null) {
                    //TODO the user has logged out
                } else {
                   //TODO the user may have logged in or changed some of his profile settings
                }
            }
        };
        profileTracker.startTracking();
        tokenTracker.startTracking();
    }

    @Override
    public View onCreateView(LayoutInflater inflater,
                             ViewGroup container,
                             Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.fragment_login_facebook, container, false);
        return rootView;
    }

    private CallbackManager mCallbackManager;
    private FacebookCallback<LoginResult> mCallback = new FacebookCallback<LoginResult>() {
        @Override
        public void onSuccess(LoginResult loginResult) {
            //TODO, implement onSuccess
        }

        @Override
        public void onCancel() {
            //TODO, implement onCancel
        }

        @Override
        public void onError(FacebookException e) {
            //TODO, implement onError inorder to handle the errors
        }
    };

    @Override
    public void onViewCreated(View view, Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        loginButton = (LoginButton) view.findViewById(R.id.btn_facebookLogin);
        //TODO: get some more permissions from the user
        //loginButton.setReadPermissions("user_friends");
        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);
    }

    @Override
    public void onResume() {
        super.onResume();
        profileTracker.startTracking();
        tokenTracker.startTracking();
    }

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

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


来源:https://stackoverflow.com/questions/30314116/how-to-handle-facebook-android-sdk-v4-1-access-token-between-activities

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!