分享对话框无法正常工作

6
我可以帮您进行翻译。以下是关于我的分享对话框代码的片段:

TabFour.java

public class TabFour extends Fragment {
private UiLifecycleHelper uiHelper;
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    View rootView = inflater.inflate(R.layout.activity_tab_four, container, false);

    return rootView;
}

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    uiHelper = new UiLifecycleHelper(getActivity(), callback);
    uiHelper.onCreate(savedInstanceState);

    OpenGraphAction action = GraphObject.Factory.create(OpenGraphAction.class);
    action.setProperty("book", "https://example.com/book/Snow-Crash.html");

    @SuppressWarnings("deprecation")
    FacebookDialog shareDialog = new FacebookDialog.OpenGraphActionDialogBuilder(getActivity(), action, "books.reads", "book")
            .build();
    uiHelper.trackPendingDialogCall(shareDialog.present());
}


@Override
public 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.i("Activity", "Success!");
        }
    });
}



@Override
public void onResume() {
    super.onResume();
    uiHelper.onResume();
}

@Override
public void onSaveInstanceState(Bundle outState) {
    super.onSaveInstanceState(outState);
    uiHelper.onSaveInstanceState(outState);
}

@Override
public void onPause() {
    super.onPause();
    uiHelper.onPause();
}

@Override
public void onDestroy() {
    super.onDestroy();
    uiHelper.onDestroy();
}
private Session.StatusCallback callback = new Session.StatusCallback() {


    @Override
    public void call(Session session, SessionState state,
            Exception exception) {
        // TODO Auto-generated method stub

    }
};

Facebook分享在模拟器上可以加载,但在手机上无法加载。当分享对话框在模拟器上加载时,您无法分享它,因为左上角的分享按钮被禁用(可见但无法单击)。此外,模拟器显示了一个toast消息“无法获取分享预览”。请帮忙解决!

我希望有人能帮我解释这个问题。谢谢!


你的模拟器上有Facebook应用吗?还要检查一下Facebook应用的版本。你看到任何ADB错误日志了吗? - 楊惟鈞
1个回答

0

来自https://developers.facebook.com/docs/android/share

Facebook分享对话框仅在安装了Facebook应用程序时才能正常工作。因此,建议的方法是首先检查是否可以呈现共享对话框,如果可以呈现,则使用您的内容呈现它或使用Web对话框呈现共享屏幕。

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
}

这里是 else 选项的示例

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))
        .setOnCompleteListener(new OnCompleteListener() {

            @Override
            public void onComplete(Bundle values,
                FacebookException error) {
                if (error == null) {
                    // When the story is posted, echo the success
                    // and the post Id.
                    final String postId = values.getString("post_id");
                    if (postId != null) {
                        Toast.makeText(getActivity(),
                            "Posted story, id: "+postId,
                            Toast.LENGTH_SHORT).show();
                    } else {
                        // User clicked the Cancel button
                        Toast.makeText(getActivity().getApplicationContext(), 
                            "Publish cancelled", 
                            Toast.LENGTH_SHORT).show();
                    }
                } else if (error instanceof FacebookOperationCanceledException) {
                    // User clicked the "x" button
                    Toast.makeText(getActivity().getApplicationContext(), 
                        "Publish cancelled", 
                        Toast.LENGTH_SHORT).show();
                } else {
                    // Generic, ex: network error
                    Toast.makeText(getActivity().getApplicationContext(), 
                        "Error posting story", 
                        Toast.LENGTH_SHORT).show();
                }
            }

        })
        .build();
    feedDialog.show();
}

不要总是依赖模拟器检查程序。毕竟它只是一个“模拟器”。你可以在真实设备上轻松预览你的应用。如果模拟器无法按照预期运行,可以在真实设备上验证一下。(当然,如果你没有多个设备、屏幕尺寸等,模拟器也很好用。)
附注:你还需要在 fb 开发者网站的应用设置页面上正确设置你的密钥哈希值。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接