Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • Post in FaceBook using your Android device via feed dialog

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 269
    Comment on it

    In many applications we have to share content, links etc on Facebook. We can achieve this by using Facebook feed dialog. The following lines of code explains how to use feed/publish dialog.
    First you need to get the app id and create a meta data. Now in the 'Application' tab, add a 'Meta Data' item called com.facebook.sdk.ApplicationId, with a value of the app_id(facebook app id).
    To create facebook app id please visit this link https://developers.facebook.com/docs/android/getting-started
    Now get facebook active session

    Session session = Session.getActiveSession();
    if(session == null)
    {
        // do FB login
        doFBLogin();
    }
    else
    {
        showFeedDialog();
    }
    
    // fb login method
    private void doFBLogin()
        {
            // start Facebook Login
            Session.openActiveSession(this, true, new Session.StatusCallback() {
    
                // callback when session changes state
                @Override
                public void call(Session session, SessionState state, Exception exception) {
                    if (session.isOpened()) {               
                        showFeedDialog();
                    }
                }
            });
        }
    
    

    Now definition of showFeedDialog() method

    private void showFeedDialog() 
    {    
        Bundle params = new Bundle();
        params.putString("name", "Write heading here");     
        params.putString("caption", "Write caption here");          
        params.putString("description", "Write description here");
        params.putString("link", "Link Url");
        params.putString("picture", "picture_url");
    
                WebDialog feedDialog = (
                        new WebDialog.FeedDialogBuilder(reviewDetail,
                                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(reviewDetail,
                                                        "Posted story, id: "+postId,
                                                        Toast.LENGTH_SHORT).show();*/
                                                Toast.makeText(reviewDetail,
                                                        "Shared on FaceBook",
                                                        Toast.LENGTH_SHORT).show();
                                            } else {
                                                // User clicked the Cancel button
                                                Toast.makeText(reviewDetail.getApplicationContext(), 
                                                        "Publish cancelled", 
                                                        Toast.LENGTH_SHORT).show();
                                            }
                                        } else if (error instanceof FacebookOperationCanceledException) {
                                            // User clicked the "x" button
                                            Toast.makeText(reviewDetail.getApplicationContext(), 
                                                    "Publish cancelled", 
                                                    Toast.LENGTH_SHORT).show();
                                        } else {
                                            // Generic, ex: network error
                                            Toast.makeText(reviewDetail.getApplicationContext(), 
                                                    "Error posting story", 
                                                    Toast.LENGTH_SHORT).show();
                                        }
                                    }
    
                                })
                                .build();
                feedDialog.show();
    
    }
    
    

    Please add Internet permission in your Manifest file. Also add facebook login activity in your manifest since it is required for FB login.

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: