如何在活动之间处理 Facebook Android SDK v4.1 访问令牌
How to handle Facebook Android SDK v4.1 Access Token between Activities
当我在成功登录用户后转到另一个 activity 时,我无法访问 Facebook 访问令牌
AccessToken.getCurrentAccessToken()
我在 LogCat 中得到了这个:
{AccessToken token:ACCESS_TOKEN_REMOVED permissions:[]}
如果您使用 android 登录按钮,我的解决方案是:
要使用片段并在每个 activity 中调用它,您可以根据当前 activity 将要做什么来选择是否显示它,片段的所有方法都可以正常工作。
这是一些代码:
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();
}
}
当我在成功登录用户后转到另一个 activity 时,我无法访问 Facebook 访问令牌
AccessToken.getCurrentAccessToken()
我在 LogCat 中得到了这个:
{AccessToken token:ACCESS_TOKEN_REMOVED permissions:[]}
如果您使用 android 登录按钮,我的解决方案是: 要使用片段并在每个 activity 中调用它,您可以根据当前 activity 将要做什么来选择是否显示它,片段的所有方法都可以正常工作。
这是一些代码:
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();
}
}