在未安装本机应用的情况下在Facebook上共享

时间:2014-06-12 19:03:15

标签: android facebook facebook-sharer

我正试着在Facebook上分享一些东西。在guide之后,我设法在您拥有原生应用时完成共享。但是,当我达到这个目的时:

步骤4:使用Feed对话框作为后备。它说为了能够使用回退方法,我需要添加下面的代码

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

添加此内容后,我在getActivity()收到错误,我使用MyClass.this解决此问题。 然后,当我运行应用程序(我在启动时直接运行此方法)时,我的应用程序崩溃,我收到一些错误,例如“尝试使用未打开的会话”

任何人都知道如何在没有安装原生应用的情况下分享内容?

1 个答案:

答案 0 :(得分:0)

导致错误的原因是用户尚未通过您的应用登录Facebook。您需要在应用中的某个位置实施登录Facebook。即使用户没有安装本机应用程序,Facebook SDK也将回退到webdialog以请求用户登录。因此,您需要检查会话是否已打开(即用户是否已登录)。 / p>

Session session = Session.getActiveSession();
boolean isSessionOpen = (session != null && session.isOpened());

if(!isSessionOpen) {
    Toast.makeText(getApplicationContext(),"Please Login With Facebook First!", Toast.LENGTH_SHORT).show();
    return;
}