这是 Facebook 推荐的。我刚刚从这个链接复制了代码示例,其中还解释了如何处理uiHelper
实例。
如果您不关心对话框的结果,则可以不使用 uiHelper。
使用静态方法检查是否可以呈现对话框是谨慎的,因为它仅初始化构建器并在确实可以呈现对话框时创建对话框。
if (FacebookDialog.canPresentShareDialog(getApplicationContext(),
FacebookDialog.ShareDialogFeature.SHARE_DIALOG)) {
// Publish the post using the Share Dialog
FacebookDialog shareDialog = new FacebookDialog.ShareDialogBuilder(this)
.setLink("https://developers.facebook.com/android")
.build();
uiHelper.trackPendingDialogCall(shareDialog.present());
} else {
// Fallback. For example, publish the post using the Feed Dialog
publishFeedDialog();
}
这是提要对话框后备:
private void publishFeedDialog() {
Bundle params = new Bundle();
params.putString("name", "Facebook SDK for Android");
params.putString("caption", "Build great social apps and get more installs.");
params.putString("description", "The Facebook SDK for Android makes it easier and faster to develop Facebook integrated Android apps.");
params.putString("link", "https://developers.facebook.com/android");
params.putString("picture", "https://raw.github.com/fbsamples/ios-3.x-howtos/master/Images/iossdk_logo.png");
WebDialog feedDialog = (
new WebDialog.FeedDialogBuilder(getActivity(),
Session.getActiveSession(),
params)).build();
feedDialog.show();
}