Facebook Android SDK 3.0, how to share content without the LoginButton

Calvin Chan picture Calvin Chan · Feb 20, 2013 · Viewed 8.2k times · Source

I am now doing a little project that want to have a share button(dialog), when users click it, it will auto-login to his/her fb account and share the content they want.

After the tutorial from fb dev, my app can share content to wall, but need to login with a fblogin button before sharing.

I have read a post from stackoverflow: Android - Facebook SDK 3 - How to login programmatically without LoginButton

UPDATE: I have implement the feedDialog with onActivityResult in my project, I found that i can login and share with one button. HOWEVER, when i rebuild the app/restart my phone, i have to press the button twice to share at the first time, but become normal(press once) later.

P.S.I have implement it with shareActionProvider

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getSupportMenuInflater().inflate(R.menu.content_main, menu);
    /** Getting the actionprovider associated with the menu item whose id is share */
    mShareActionProvider = (ShareActionProvider) menu.findItem(R.id.share).getActionProvider();

    /** Getting the target intent */
    Intent intent = getDefaultShareIntent();

    /** Setting a share intent */       
    if(intent!=null){
        mShareActionProvider.setShareIntent(intent);
        mShareActionProvider.setOnShareTargetSelectedListener(new OnShareTargetSelectedListener(){
            @Override
            public boolean onShareTargetSelected(ShareActionProvider source, Intent intent) {
                if ("com.facebook.katana".equals(intent.getComponent().getPackageName())){
                    if (Session.getActiveSession() == null || Session.getActiveSession().isClosed()) {
                        Session.openActiveSession(Content.this, true, null);
                }else{
                        publishFeedDialog();
                    }
                    return true;
                }
                return false;
            }
        });
    }

    return super.onCreateOptionsMenu(menu);
}
@Override
   public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        Session.getActiveSession().onActivityResult(this, requestCode, resultCode, data);
        if (Session.getActiveSession() != null || Session.getActiveSession().isOpened())
            publishFeedDialog();
    }
private void publishFeedDialog() {
        Bundle params = new Bundle();
        params.putString("name", "ab");
        params.putString("caption", "cd");
        params.putString("description", "def");
        params.putString("link", "https://developers.facebook.com/android");
        params.putString("picture", "abc.jpg");

        WebDialog feedDialog = (
            new WebDialog.FeedDialogBuilder(Content.this,
                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(Content.this,
                                "Posted story, id: "+postId,
                                Toast.LENGTH_SHORT).show();
                        } else {
                            // User clicked the Cancel button
                            Toast.makeText(Content.this.getApplicationContext(), 
                                "Publish cancelled", 
                                Toast.LENGTH_SHORT).show();
                        }
                    } else if (error instanceof FacebookOperationCanceledException) {
                        // User clicked the "x" button
                        Toast.makeText(Content.this.getApplicationContext(), 
                            "Publish cancelled", 
                            Toast.LENGTH_SHORT).show();
                    } else {
                        // Generic, ex: network error
                        Toast.makeText(Content.this.getApplicationContext(), 
                            "Error posting story", 
                            Toast.LENGTH_SHORT).show();
                    }
                }

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

Answer

Calvin Chan picture Calvin Chan · Feb 22, 2013

Thanks Ming Li very much for the solution

I finally got the answer!!!! Below is the code, hope that it can help other developers

private Session.StatusCallback callback = new Session.StatusCallback() {
          @Override
          public void call(Session session, SessionState state,
            Exception exception) {
           onSessionStateChange(session, state, exception);
          }
    };
    private void onSessionStateChange(Session session, SessionState state, Exception exception) {
        if (state.isOpened()) {
            publishFeedDialog();
        }
    }
    //........................
                    if (Session.getActiveSession() == null || Session.getActiveSession().isClosed()) {
                            Session.openActiveSession(Content.this, true, callback);
                    }else{
                            publishFeedDialog();
                        }
     //.......................
    @Override
       public void onActivityResult(int requestCode, int resultCode, Intent data) {
            super.onActivityResult(requestCode, resultCode, data);
            Session.getActiveSession().onActivityResult(this, requestCode, resultCode, data);
        }
    private void publishFeedDialog() {
            //........................
      }