每次登出和登录时都不会调用onCurrentProfileChanged函数,但是如果我删除了profiletracker,那么第一次登录时该配置文件将为null。如何解决此问题?我已粘贴下面的代码。
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
SharedPreferenceManager.setApplicationContext(getApplicationContext());
onregister();
SharedPreferenceManager.setPreference("notificationcount", 0);
FacebookSdk.sdkInitialize(this.getApplicationContext());
callbackManager = CallbackManager.Factory.create();
LoginManager.getInstance().registerCallback(callbackManager,
new FacebookCallback<LoginResult>() {
@Override
public void onSuccess(final LoginResult loginResult) {
profileTracker = new ProfileTracker() {
@Override
protected void onCurrentProfileChanged(Profile oldProfile, Profile currentProfile) {
Profile.setCurrentProfile(currentProfile);
profileTracker.stopTracking();
GraphRequest request = GraphRequest.newMeRequest(
AccessToken.getCurrentAccessToken(),
new GraphRequest.GraphJSONObjectCallback() {
@Override
public void onCompleted(
JSONObject jsonObject,
GraphResponse response) {
try {
dismissProgressDialog();
final Profile profile = Profile.getCurrentProfile();
SharedPreferenceManager.setPreference("id", profile.getId());
email = jsonObject.getString("email");
gender = jsonObject.getString("gender");
if (mAuthTask == null) {
mAuthTask = new UserLoginTask(email, "", profile.getName(), "facebook", profile.getId(), AccessToken.getCurrentAccessToken().getToken(), gender, getRegistrationId(getApplicationContext()));
//Log.v("token", loginResult.getAccessToken().getToken());
mAuthTask.execute((Void) null);
}
} catch (JSONException exe) {
}
}
});
Bundle parameters = new Bundle();
parameters.putString("fields", "id,name,link,email,gender");
request.setParameters(parameters);
request.executeAsync();
}
};
profileTracker.startTracking();
}
@Override
public void onCancel() {
dismissProgressDialog();
LoginManager.getInstance().logOut();
}
@Override
public void onError(FacebookException exception) {
dismissProgressDialog();
LoginManager.getInstance().logOut();
}
});
答案 0 :(得分:0)
您是否在代码中添加了这一行
LoginManager.getInstance().logInWithReadPermissions(this, Arrays.asList("public_profile,email"));
在调用registerCallback之前?