当我第一次通过 Facebook 登录应用程序时,我从 Profile.getCurrentProfile();
获取个人资料
当我退出应用程序并再次启动时,它已经登录。所以我可以直接调用 Profile.getCurrentProfile();
返回 null。
代码
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
FacebookSdk.sdkInitialize(getApplicationContext());
setContentView(R.layout.activity_page);
Profile profile = Profile.getCurrentProfile();
// For the first launch the profile will be null
displayProfileName(profile);
LoginButton loginButton = (LoginButton) findViewById(R.id.login_button);
loginButton.setReadPermissions("public_profile");
callbackManager = CallbackManager.Factory.create();
loginButton.registerCallback(callbackManager,
new FacebookCallback<LoginResult>() {
@Override
public void onSuccess(final LoginResult loginResult) {
profileTracker = new ProfileTracker() {
@Override
protected void onCurrentProfileChanged(
Profile oldProfile, Profile currentProfile) {
profileTracker.stopTracking();
Profile.setCurrentProfile(currentProfile);
Profile profile = Profile.getCurrentProfile();
displayProfileName(profile);
}
};
profileTracker.startTracking();
}
@Override
public void onCancel() {
}
@Override
public void onError(FacebookException exception) {
}
});
}
@Override
protected void onDestroy() {
super.onDestroy();
if (profileTracker != null) {
profileTracker.stopTracking();
}
}
@Override
protected void onPause() {
super.onPause();
// Logs 'app deactivate' App Event.
AppEventsLogger.deactivateApp(this);
}
@Override
protected void onRestart() {
super.onRestart();
AppEventsLogger.activateApp(this);
}
/**
*
* Method to display the Profile Name
*/
private void displayProfileName(Profile profile) {
if (profile != null) {
Toast.makeText(MainActivity.this, profile.getName(),
Toast.LENGTH_LONG).show();
} else {
Toast.makeText(MainActivity.this, "No Profile", Toast.LENGTH_LONG)
.show();
}
}
@Override
protected void onActivityResult(int arg0, int arg1, Intent arg2) {
super.onActivityResult(arg0, arg1, arg2);
callbackManager.onActivityResult(arg0, arg1, arg2);
}
最佳答案
这有两种情况:
- 首次登录,请关注 my other answer .
- 对于第二次登录,您需要刷新您的
AccessToken
,然后获取配置文件。刷新 token 代码可以在this answer中找到但是下面的代码有它(为了简化)。
代码取自FB's dreadful documentation ).
您可以将此代码直接放入您的应用程序中,其中注释为“案例 1”,只需调用您的正常 FB 登录即可。
private AccessTokenTracker mAccessTokenTracker;
private void loginToMyFbApp() {
FacebookSdk.sdkInitialize(this);
if (AccessToken.getCurrentAccessToken() != null) {
mAccessTokenTracker = new AccessTokenTracker() {
@Override
protected void onCurrentAccessTokenChanged(AccessToken oldAccessToken, AccessToken currentAccessToken) {
mAccessTokenTracker.stopTracking();
if(currentAccessToken == null) {
//(the user has revoked your permissions -
//by going to his settings and deleted your app)
//do the simple login to FaceBook
//case 1
}
else {
//you've got the new access token now.
//AccessToken.getToken() could be same for both
//parameters but you should only use "currentAccessToken"
//case 2
fetchProfile();
}
}
};
mAccessTokenTracker.startTracking();
AccessToken.refreshCurrentAccessTokenAsync();
}
else {
//do the simple login to FaceBook
//case 1
}
}
private void fetchProfile() {
GraphRequest request = GraphRequest.newMeRequest(
AccessToken.getCurrentAccessToken(),
new GraphRequest.GraphJSONObjectCallback() {
@Override
public void onCompleted(JSONObject object, GraphResponse response) {
// this is where you should have the profile
Log.v("fetched info", object.toString());
}
});
Bundle parameters = new Bundle();
parameters.putString("fields", "id,name,link"); //write the fields you need
request.setParameters(parameters);
request.executeAsync();
}
关于android - 首次登录后,Facebook Profile.getCurrentProfile() 始终返回 null,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33252401/