Android Facebook SDK 3.0 Simple Status Update Without Explicit Login?

无人久伴 提交于 2019-12-05 04:10:13

问题


I'd like to know the simplest way to update a user's facebook status. I'd like to simply display a 'Share' button. When the user touches 'Share':

  1. If they are already logged in to Facebook, a dialog will appear giving them the option to edit their posting before submitting to Facebook.

  2. If they aren't already logged in to Facebook, the will be prompted to login and then automatically shown the sharing dialog upon successful login.

Is there a simple way to do this, or do I need to implement a separate facebook login flow?


回答1:


So I think I've got a pretty clean way to do it. I welcome any comments.

I'm posting my entire Facebook Manager class which does the work to login and also to bring up the feed dialog:

public class ManagerFacebook
{
    private Activity mActivity;

    public void updateStatus(final String name, final String caption, final String description, final String link, final String urlPicture,
        final Activity activity, final ListenerShareFacebook listenerShareFacebook)
    {
        mActivity = activity;

        // start Facebook Login
        Session.openActiveSession(activity, true, new Session.StatusCallback()
        {
            // callback when session changes state
            public void call(final Session session, SessionState state, Exception exception)
            {
                if (session.isOpened())
                {
                    publishFeedDialog(name, caption, description, link, urlPicture, listenerShareFacebook);
                }

            }
        });
    }

    private void publishFeedDialog(String name, String caption, String description, String link, String urlPicture,
        final ListenerShareFacebook listenerShareFacebook)
    {
        Bundle params = new Bundle();
        params.putString("name", name);
        params.putString("caption", caption);
        params.putString("description", description);
        params.putString("link", link);
        params.putString("picture", urlPicture);

        Session session = Session.getActiveSession();

        WebDialog feedDialog = (new WebDialog.FeedDialogBuilder(mActivity, session, params)).setOnCompleteListener(new OnCompleteListener()
        {
            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)
                    {
                        listenerShareFacebook.onShareFacebookSuccess();
                    }
                    else
                    {
                        // User clicked the Cancel button
                        listenerShareFacebook.onShareFacebookFailure("Publish cancelled");
                    }
                }
                else if (error instanceof FacebookOperationCanceledException)
                {
                    // User clicked the "x" button
                    listenerShareFacebook.onShareFacebookFailure("Publish cancelled");
                }
                else
                {
                    // Generic, ex: network error
                    listenerShareFacebook.onShareFacebookFailure("Error posting story");
                }
            }

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

Also, referenced above is my Listener interface so I can notify my Activity of Facebook related stuff:

public interface ListenerShareFacebook
{
    void onShareFacebookSuccess();

    void onShareFacebookFailure(String error);
}



回答2:


I would really recommend this:

Intent sharingIntent = new Intent(Intent.ACTION_SEND);
         sharingIntent.setType("text/plain");
         sharingIntent.putExtra(android.content.Intent.EXTRA_TEXT, "text");
         sharingIntent.putExtra(android.content.Intent.EXTRA_SUBJECT, "Title");
         startActivity(Intent.createChooser(sharingIntent, "Share using"));

This doesn't use facebook sdk but it does use facebook app. In my view most of the people that have smartphones and facebook use this app, and expect to share everything through this app. Also users might click to share on something else (twitter, sms, g+, email,...)

For me the best supstitute to any facebook sdk. (you can also attach pictures)



来源:https://stackoverflow.com/questions/16020771/android-facebook-sdk-3-0-simple-status-update-without-explicit-login

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!