如何在 android 中为 facebook sdk 添加注销回调
How to add a logout callback for facebook sdk in android
我已将 Facebook SDK 集成到我的 android 应用程序中。如手册中所述,我为 facebook 添加了登录回调。但是如果用户从 facebook 注销,我必须更改 UI。我将该代码放在哪里。我的登录密码是
/**
* Login Callback for facebook login
*/
callbackManager = CallbackManager.Factory.create();
LoginManager.getInstance().registerCallback(callbackManager,
new FacebookCallback<LoginResult>() {
@Override
public void onSuccess(LoginResult loginResult) {
//Call updateUI()
setData("provider","facebook");
loginType = LoginTypes.FB_LOGIN;
isLoggedin = true;
GraphRequest request = GraphRequest.newMeRequest(
loginResult.getAccessToken(),
new GraphRequest.GraphJSONObjectCallback() {
@Override
public void onCompleted(
JSONObject object,
GraphResponse response) {
// Application code
txtName.setText(response.toString());
updateUI();
}
});
Bundle parameters = new Bundle();
parameters.putString("fields", "id,name,email");
request.setParameters(parameters);
request.executeAsync();
}
@Override
public void onCancel() {
editText_message.setText("Login Cancelled.");
// App code
}
@Override
public void onError(FacebookException exception) {
// App code
}
});
有两种可能的方法:
1) 您需要像这样在创建 AccessTokenTracker 时进行覆盖:
accessTokenTracker = new AccessTokenTracker() {
@Override
protected void onCurrentAccessTokenChanged(AccessToken oldAccessToken,
AccessToken currentAccessToken) {
if (currentAccessToken == null) {
//write your code here what to do when user logout
}
}
}
2) 你可以调用LoginManager.logOut()注销用户
希望这对您有所帮助:)
这对我有用:-
profileTracker = new ProfileTracker() {
@Override
protected void onCurrentProfileChanged(
Profile oldProfile,
Profile currentProfile) {
if(currentProfile == null){
tvUNameandEmail.setText(R.string.app_name);
}
}
};
谢谢斯坦。你帮我解决了,但花了我一些时间。为了帮助其他人,这就是完整的代码:
Profile fbProfile = Profile.getCurrentProfile();
AccessTokenTracker accessTokenTracker = new AccessTokenTracker() {
@Override
protected void onCurrentAccessTokenChanged(AccessToken oldAccessToken,
AccessToken currentAccessToken) {
if (currentAccessToken == null) {
Log.d(TAG, "onLogout catched");
deleteContact();//This is my code
}
}
};
if (fbProfile == null) {
Log.d(TAG, "NOT logged in");
callbackManager = CallbackManager.Factory.create();
LoginButton loginButton = (LoginButton) findViewById(R.id.login_button);
loginButton.setReadPermissions("email");
// Callback registration
loginButton.registerCallback(callbackManager, new FacebookCallback<LoginResult>() {
@Override
public void onSuccess(LoginResult loginResult) {
Log.d(TAG, "onSuccess login Facebook");
GraphRequest request = GraphRequest.newMeRequest(
AccessToken.getCurrentAccessToken(),
new GraphRequest.GraphJSONObjectCallback() {
@Override
public void onCompleted(JSONObject object, GraphResponse response) {
FacebookSdk.setIsDebugEnabled(true);
FacebookSdk.addLoggingBehavior(LoggingBehavior.INCLUDE_ACCESS_TOKENS);
Log.d(TAG, "AccessToken.getCurrentAccessToken() " + AccessToken.getCurrentAccessToken().toString());
Profile profile = Profile.getCurrentProfile();
Log.d(TAG, "Current profile: " + profile);
if (profile != null) {
Log.d(TAG, String.format("id = %s; name = %s; lastName = %s; uri = %s",
profile.getId(), profile.getFirstName(),
profile.getLastName(), profile.getProfilePictureUri(50, 60)));
name = String.format("%s %s",profile.getFirstName(),profile.getLastName());
fbid = profile.getId();
pushNewContact();//This is my code
}
}
});
request.executeAsync();
}
@Override
public void onCancel() {
Log.d(TAG, "onCancel");
}
@Override
public void onError(FacebookException e) {
Log.e(TAG, "onError", e);
}
});
} else {
Log.d(TAG, "Logged with " + fbProfile.getName());
fbid = fbProfile.getId();
}
accessTokenTracker.startTracking();
我已将 Facebook SDK 集成到我的 android 应用程序中。如手册中所述,我为 facebook 添加了登录回调。但是如果用户从 facebook 注销,我必须更改 UI。我将该代码放在哪里。我的登录密码是
/**
* Login Callback for facebook login
*/
callbackManager = CallbackManager.Factory.create();
LoginManager.getInstance().registerCallback(callbackManager,
new FacebookCallback<LoginResult>() {
@Override
public void onSuccess(LoginResult loginResult) {
//Call updateUI()
setData("provider","facebook");
loginType = LoginTypes.FB_LOGIN;
isLoggedin = true;
GraphRequest request = GraphRequest.newMeRequest(
loginResult.getAccessToken(),
new GraphRequest.GraphJSONObjectCallback() {
@Override
public void onCompleted(
JSONObject object,
GraphResponse response) {
// Application code
txtName.setText(response.toString());
updateUI();
}
});
Bundle parameters = new Bundle();
parameters.putString("fields", "id,name,email");
request.setParameters(parameters);
request.executeAsync();
}
@Override
public void onCancel() {
editText_message.setText("Login Cancelled.");
// App code
}
@Override
public void onError(FacebookException exception) {
// App code
}
});
有两种可能的方法:
1) 您需要像这样在创建 AccessTokenTracker 时进行覆盖:
accessTokenTracker = new AccessTokenTracker() {
@Override
protected void onCurrentAccessTokenChanged(AccessToken oldAccessToken,
AccessToken currentAccessToken) {
if (currentAccessToken == null) {
//write your code here what to do when user logout
}
}
}
2) 你可以调用LoginManager.logOut()注销用户
希望这对您有所帮助:)
这对我有用:-
profileTracker = new ProfileTracker() {
@Override
protected void onCurrentProfileChanged(
Profile oldProfile,
Profile currentProfile) {
if(currentProfile == null){
tvUNameandEmail.setText(R.string.app_name);
}
}
};
谢谢斯坦。你帮我解决了,但花了我一些时间。为了帮助其他人,这就是完整的代码:
Profile fbProfile = Profile.getCurrentProfile();
AccessTokenTracker accessTokenTracker = new AccessTokenTracker() {
@Override
protected void onCurrentAccessTokenChanged(AccessToken oldAccessToken,
AccessToken currentAccessToken) {
if (currentAccessToken == null) {
Log.d(TAG, "onLogout catched");
deleteContact();//This is my code
}
}
};
if (fbProfile == null) {
Log.d(TAG, "NOT logged in");
callbackManager = CallbackManager.Factory.create();
LoginButton loginButton = (LoginButton) findViewById(R.id.login_button);
loginButton.setReadPermissions("email");
// Callback registration
loginButton.registerCallback(callbackManager, new FacebookCallback<LoginResult>() {
@Override
public void onSuccess(LoginResult loginResult) {
Log.d(TAG, "onSuccess login Facebook");
GraphRequest request = GraphRequest.newMeRequest(
AccessToken.getCurrentAccessToken(),
new GraphRequest.GraphJSONObjectCallback() {
@Override
public void onCompleted(JSONObject object, GraphResponse response) {
FacebookSdk.setIsDebugEnabled(true);
FacebookSdk.addLoggingBehavior(LoggingBehavior.INCLUDE_ACCESS_TOKENS);
Log.d(TAG, "AccessToken.getCurrentAccessToken() " + AccessToken.getCurrentAccessToken().toString());
Profile profile = Profile.getCurrentProfile();
Log.d(TAG, "Current profile: " + profile);
if (profile != null) {
Log.d(TAG, String.format("id = %s; name = %s; lastName = %s; uri = %s",
profile.getId(), profile.getFirstName(),
profile.getLastName(), profile.getProfilePictureUri(50, 60)));
name = String.format("%s %s",profile.getFirstName(),profile.getLastName());
fbid = profile.getId();
pushNewContact();//This is my code
}
}
});
request.executeAsync();
}
@Override
public void onCancel() {
Log.d(TAG, "onCancel");
}
@Override
public void onError(FacebookException e) {
Log.e(TAG, "onError", e);
}
});
} else {
Log.d(TAG, "Logged with " + fbProfile.getName());
fbid = fbProfile.getId();
}
accessTokenTracker.startTracking();