共享对话框无法运行

时间:2013-10-21 08:19:53

标签: android facebook-graph-api share

我在片段中的分享对话框中有以下代码:

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

    }
};

}

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

我希望有人可以向我解释一下。谢谢!

1 个答案:

答案 0 :(得分:0)

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

Facebook分享对话框仅在安装了facebook app时才有效。因此,建议的方法是首先检查是否可以显示共享对话框,如果可以显示,则将其与您的内容一起显示,或使用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
}

这是其他选项的例子

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();
}

并且不要总是依靠模拟器来检查事情。毕竟它只是一个"模拟器" 。您可以在真实设备上轻松预览应用。因此,如果事情无法在模拟器上运行,请在真实设备上进行验证。 (当然,我同意,当你没有多个设备,屏幕尺寸等时,模拟器很好。)

PS:您还需要在fb开发者网站的应用设置页面中正确设置Key Hashes。