在Android Facebook SDK中使用共享对话框.如何知道用户实际共享或取消共享活动?

Eli*_*ova 14 android facebook facebook-android-sdk android-facebook

我已经为Android应用添加了共享功能,如此处所述https://developers.facebook.com/docs/android/share-dialog/#setup

但我注意到,如果用户被取消,onComplete则无论如何都要调用共享活动

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);

    uiHelper.onActivityResult(requestCode, resultCode, data, new FacebookDialog.Callback() {
        @Override
        public void onError(FacebookDialog.PendingCall pendingCall, Exception error, Bundle data) {
            Log.e("Activity", String.format("Error: %s", error.toString()));
        }

        @Override
        public void onComplete(FacebookDialog.PendingCall pendingCall, Bundle data) {
            Log.e("Activity", "Success!");
        }
    });
}
Run Code Online (Sandbox Code Playgroud)

我也查看了返回的Bundle.即使我取消分享对话,我也得到了

com.facebook.platform.extra.DID_COMPLETE=true
Run Code Online (Sandbox Code Playgroud)

如何获得用户真正在Facebook上共享数据的结果?(不用facebook按钮单独登录.也许需要添加一些权限?)

Min*_* Li 7

请参阅https://developers.facebook.com/docs/android/share-dialog/#handling-responses

您可以通过呼叫判断用户是否已取消

String gesture = FacebookDialog.getNativeDialogCompletionGesture(data);
if (gesture != null) {
  if ("post".equals(gesture)) {
    // the user hit Post
  } else if ("cancel".equals(gesture)) {
    // the user hit cancel
  } else {
    // unknown value
  }
} else {
  // either an error occurred, or your app has never been authorized
}
Run Code Online (Sandbox Code Playgroud)

其中data是结果包.但是,如果用户通过您的应用程序登录(即您至少具有basic_info权限),它将仅返回非空值.如果用户从未登录或授权您的应用程序,那么您将看到的唯一内容是DID_COMPLETE,除非发生错误,否则它将始终为true.这是设计的.


JHN*_*ves 5

为了获得共享的结果,您的应用至少需要具有basic_info权限.

要解决这个问题,只需打开一个会话(这将自动请求basic_info权限):

Session.openActiveSession(this /*your activity*/, 
                          true /*allows the UI login to show up if needed*/, 
                          new Session.StatusCallback() {
    @Override
    public void call(Session session, SessionState state, Exception exception) {
        Log.i("[Facebook]", "Session: " + state.toString());
        if (session.isOpened()) {
           /// now you are good to get the sharing results
        }
    }
});
Run Code Online (Sandbox Code Playgroud)

您可以在此处找到更多信息:https://developers.facebook.com/docs/android/getting-started/