我正在使用以下代码在Facebook上分享链接。当用户在“共享”对话框界面上单击“取消”时,有时会调用onSuccess()回调方法而不是onCancel()。并获得post id null。请帮我解决问题?
ShareButton btn;
CallbackManager callbackManager;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
FacebookSdk.sdkInitialize(this);
callbackManager = CallbackManager.Factory.create();
setContentView(R.layout.activity_share);
btn = (ShareButton) findViewById(R.id.btn_share);
btn.registerCallback(callbackManager, new FacebookCallback<Sharer.Result>() {
@Override
public void onSuccess(Sharer.Result result) {
Log.e("Tag","Successfully posted");
Log.e("Post id",result.getPostId());
}
@Override
public void onCancel() {
Log.e("Tag","Canceled by user");
}
@Override
public void onError(FacebookException error) {
Log.e("Tag",error.getLocalizedMessage());
}
});
ShareLinkContent content = new ShareLinkContent.Builder()
.setContentUrl(Uri.parse("My Custom URL"))
.setContentTitle("Test")
.build();
btn.setShareContent(content);
}
@Override
protected void onActivityResult(final int requestCode, final int resultCode, final Intent data) {
super.onActivityResult(requestCode, resultCode, data);
callbackManager.onActivityResult(requestCode, resultCode, data);
}
答案 0 :(得分:3)
好吧,也许我迟到了,但几周前我遇到了同样的问题。 我注意到的是,如果在基于Web的共享对话框中按下取消,则会调用onSuccess()方法,但Share.Result对象包含null postId,因此您可以通过检查用户按下取消或共享来控制Share.Result回应。
我注意到的另一件事是,如果您与安装的本机应用程序共享内容,则postId字段始终为null ...因此您必须检查用户是否安装了应用程序以检查postId字段。< / p>