How do I simply share content on Facebook wall using its Android SDK?

前端 未结 2 829
予麋鹿
予麋鹿 2021-02-04 15:38

I cannot find many examples of the FB + Android SDK and the samples are not simple enough(some of them are deprecated). My simple goal is to share some content on FB using my An

2条回答
  •  南方客
    南方客 (楼主)
    2021-02-04 16:13

    Taken from Share on User's Wall using Facebook SDK:

    private void share() {
        Bundle bundle = new Bundle();
        bundle.putString("caption", "Harlem Shake Launcher for Android");
        bundle.putString("description", "Your android can do the Harlem Shake. Download it from google play");
        bundle.putString("link", "https://play.google.com/store/apps/details?id=mobi.shush.harlemlauncher");
        bundle.putString("name", "Harlem Shake Launcher");
        bundle.putString("picture", "http://shush.mobi/bla.png");
        new WebDialog.FeedDialogBuilder(mContext, mySession, bundle).build().show();
    }
    

    If you need to login (add this in you activity wherever you need to login/share):

    Session.openActiveSession(this, true, new Session.StatusCallback() {
    
        @Override
        public void call(Session session, SessionState state, Exception exception) {
            if(session.isOpened()) {
                share();
            }
        }
    });
    

    You will need to add to your activity:

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        switch(requestCode) {
        default:
            if(Session.getActiveSession() != null) //I need to check if this null just to sleep peacefully at night
                Session.getActiveSession().onActivityResult(this, requestCode, resultCode, data);
            break;
        }
    }
    

提交回复
热议问题