我正在开发一款兼具Fb Like
& Share
按钮。直到现在我只添加了Like按钮。我想在用户喜欢(点击Like按钮)时做一些事情,所以我需要注册callback
方法。所以我想知道如何实现这一点,并且当点击Like按钮时,分享选项也会显示,那么我该如何处理呢。
以下是我的一些代码:
LinearLayout btnLoginToLike;
LikeView likeView;
CallbackManager callbackManager;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initInstances();
initCallbackManager();
refreshButtonsState();
}
private void initInstances() {
btnLoginToLike = (LinearLayout) findViewById(R.id.btnLoginToLike);
likeView = (LikeView) findViewById(R.id.likeView);
likeView.setLikeViewStyle(LikeView.Style.STANDARD);
likeView.setAuxiliaryViewPosition(LikeView.AuxiliaryViewPosition.INLINE);
likeView.setObjectIdAndType(
"http://inthecheesefactory.com/blog/understand-android-activity-launchmode/en",
LikeView.ObjectType.OPEN_GRAPH);
btnLoginToLike.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
LoginManager.getInstance().logInWithReadPermissions(MainActivity.this, Arrays.asList("public_profile"));
}
});
}
private void initCallbackManager() {
callbackManager = CallbackManager.Factory.create();
LoginManager.getInstance().registerCallback(callbackManager, new FacebookCallback<LoginResult>() {
@Override
public void onSuccess(LoginResult loginResult) {
refreshButtonsState();
}
@Override
public void onCancel() {
}
@Override
public void onError(FacebookException e) {
}
});
}
private void refreshButtonsState() {
if (!isLoggedIn()) {
btnLoginToLike.setVisibility(View.VISIBLE);
likeView.setVisibility(View.GONE);
} else {
btnLoginToLike.setVisibility(View.GONE);
likeView.setVisibility(View.VISIBLE);
}
}
public boolean isLoggedIn() {
return AccessToken.getCurrentAccessToken() != null;
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
// Handle Facebook Login Result
callbackManager.onActivityResult(requestCode, resultCode, data);
}
答案 0 :(得分:1)
在onActivityResults上添加一些行。
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
callbackManager.onActivityResult(requestCode, resultCode, data);
if (resultCode == RESULT_OK && data != null) {
Bundle bundle = data.getExtras().getBundle("com.facebook.platform.protocol.RESULT_ARGS");
if (bundle != null) {
Logger.LogInfo("Bundle", bundle.toString());
Boolean isLiked = bundle.getBoolean("object_is_liked"); // liked/unliked
Logger.LogInfo("object_is_liked", isLiked + "");
Integer isCompleted = bundle.getInt("didComplete");
Logger.LogInfo("didComplete", isCompleted + "");
Integer count = bundle.getInt("like_count"); // object like count
Logger.LogInfo("like_count", count + "");
String likeCount = bundle.getString("like_count_string");
Logger.LogInfo("like_count_string", likeCount + "");
String social = bundle.getString("social_sentence");
Logger.LogInfo("social_sentence", social + "");
String completeGesture = bundle.getString("completionGesture"); // liked/cancel/unliked
Logger.LogInfo("completionGesture", completeGesture + "");
}
}
}