Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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();
        }
like image 373
Calvin Chan Avatar asked Feb 20 '13 08:02

Calvin Chan


People also ask

How do I share a video on Facebook Android programmatically?

ShareVideo = new ShareVideo. Builder() . setLocalUrl(videoUrl) . build(); ShareVideoContent content = new ShareVideoContent.

How did you integrate the Facebook SDK in Android app?

To use the Facebook SDK in an Android Studio project, add the SDK as a build dependency and import the SDK. Go to Android Studio | New Project | Minimum SDK. Select API 15: Android 4.0. 3 (IceCreamSandwich) or higher and create your new project.


2 Answers

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() {
            //........................
      }
like image 109
Calvin Chan Avatar answered Nov 17 '22 15:11

Calvin Chan


I think the issue here is that when you're building the menu, you check to see if the active session is null, and if it is, you call openActiveSession, and wait for onActivityResult to be called.

This is all correct, HOWEVER, if the user has previously authorized your app, then the access tokens, etc, are all saved in a token cache, and calling openActiveSession will actually open the session immediately (without calling onActivityResult).

The real correct way is to pass in a StatusCallback to your open call (rather than null), and in that call back, check to see if the session is open, and call your publishFeedDialog method.

like image 45
Ming Li Avatar answered Nov 17 '22 14:11

Ming Li