Wednesday, 23 October 2013

Simple Facebook SDK for Android which wraps original Facebook SDK 3.5

Hello Friends ,

Today i am going to show you the library which is named "SimpleFacebook Library" which contains the functionality of  Sharing,Inviting Friends ,Publishing , Post Feeds ,get Profile Details, Get Selected Friends List, Send invitation to Friends, get Album List  etc.. 

This library  include the Facebook SDK 3.5 which  makes the life much easier by coding less code for being able to login, publish feeds and open graph stories, invite friends and more.

Here are the Features supported by SimpleFacebook Library:

Features
  • Login
  • Logout
  • Publish feed
  • Publish story (open graph)
  • Publish photo
  • Invite friend/s
    • Invite all friends
    • Invite suggested friends
    • Invite one friend
  • Get profile
  • Get friends
  • Get albums
  • Based on latest Facebook SDK
  • Permission strings are predefined
  • No need to use LoginButton view for being able to login/logout. You can use any View.
  • No need to care for correct login with READ and PUBLISH permissions. Just mention the permissions you need and this library will care for the rest.

This library has reduced the login process of facebook using sdk. Using Facebook SDK we were calling the facebook login button to request for login. Now, You can call login(Activity) method on click of any View and you don't need to use LoginButton.

You just need to create the SimpleFacebook class instance in your activity's onResume() method and using instance call the login method of it as below:

Login:


mSimpleFacebook.login(onLoginListener);

Same way the logout functionality has also became simpler. To logout from Facebook you just have to call the logout method as below:

Logout:

 mSimpleFacebook.logout(onLogoutListener);

Project Configuration

First of all you need to download the Facebook SDK 3.5  or Download SDK  Which will provide all the latest classes which are used in SimpleFacebook library.

Steps:

  • After downloading the Facebook SDK just import the FB library in your workspace. 
  • Also import the SimpleFacebook library and SimpleFacebookSample in your workspace.
  • Once you have imported the SimpleFacebook Library in your workspace you need to Add reference fromSimple Facebook project toFacebookSDK project.

  • Now, you can add reference from your app to Simple Facebook project.
  • Update the manifest.xml of your application and add lines:


<uses-permission android:name="android.permission.INTERNET" />

<activity
    android:name="com.facebook.LoginActivity"
    android:label="@string/app_name"
    android:theme="@android:style/Theme.Translucent.NoTitleBar" />
  • In your Application or Activity class you need to define the list of permissions which you are going to use to access the details from Facebook.
  • Define and select permissions you need:     
Permissions[] permissions = new Permissions[] {
    Permissions.USER_PHOTOS,
    Permissions.EMAIL,
    Permissions.PUBLISH_ACTION
};
  • Build and define the configuration by putting app_idnamespace andpermissions:
    SimpleFacebookConfiguration configuration = new SimpleFacebookConfiguration.Builder()
        .setAppId("***************")
        .setNamespace("<namespace>")
        .setPermissions(permissions)
        .build();
    
  • And, set this configuration:
    SimpleFacebook.setConfiguration(configuration);  
    
    There is no need to set the configuration in any activity, it should be done just once.
      After defining the Facebook Configuration you can start implementation as below:


  1. onResume()

       First of all in your each Activity where you want to use the SimpleFacebook library ,you just need to          create the instance of  SimpleFacebook Class in you activity's onResume() method.

@Override
public void onResume()
{
    super.onResume();
    mSimpleFacebook = SimpleFacebook.getInstance(this);
}
   
   2. onActivityResult()

      In your onActivityResult method add the below line:
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data)
{
    mSimpleFacebook.onActivityResult(this, requestCode, resultCode, data); 
    super.onActivityResult(requestCode, resultCode, data);
} 

Now, i am going to show you How to implement the login functionality using library. 
After successfully following the above all steps you need to implement the LoginListener interface in your activity which you can call directly on button click or from anywhere from any event.


// login listener
OnLoginListener onLoginListener = new SimpleFacebook.OnLoginListener()
{

    @Override
    public void onFail(String reason)
    {
        Log.w(TAG, reason);
    }

    @Override
    public void onException(Throwable throwable)
    {
        Log.e(TAG, "Bad thing happened", throwable);
    }

    @Override
    public void onThinking()
    {
        // show progress bar or something to the user while login is happening
        Log.i(TAG, "In progress");
    }

    @Override
    public void onLogin()
    {
        // change the state of the button or do whatever you want
        Log.i(TAG, "Logged in");
    }

    @Override
    public void onNotAcceptingPermissions()
    {
        Log.w(TAG, "User didn't accept read permissions");
    }

};
mButtonLogin.setOnClickListener(new View.OnClickListener() {
   @Override
   public void onClick(View arg0) {
      mSimpleFacebook.login(onLoginListener);
   }
  });

This will open the Facebook login dialog from where you can login into facebook application and after login sucessfully it will redirect you to  your application again.

Same for the logout from Facebook you need to implement the LogoutListener as below:
Set OnLogoutListener and call for logout(OnLogoutListener) to disconnect from facebook.

// logout listener
OnLogoutListener onLogoutListener = new SimpleFacebook.OnLogoutListener()
{

    @Override
    public void onFail(String reason)
    {
        Log.w(TAG, reason);
    }

    @Override
    public void onException(Throwable throwable)
    {
        Log.e(TAG, "Bad thing happened", throwable);
    }

    @Override
    public void onThinking()
    {
        // show progress bar or something to the user while login is happening
        Log.i(TAG, "In progress");
    }

    @Override
    public void onLogout()
    {
        Log.i(TAG, "You are logged out");
    }

};
mButtonLogout.setOnClickListener(new View.OnClickListener() {
   @Override
   public void onClick(View arg0) {
                        mSimpleFacebook.logout(onLogoutListener);
   }
  });

Now i am going  to show you how you can publish your post on Facebook.


To publish feed on facebook there is onPublishListener which you just need to implement. Also the Feed class which you can use to add the details of feeds. 

Set OnPublishListener and call for publish(Feed, OnPublishListener).

Basic properties

  • message - The message of the user
  • name - The name of the link attachment
  • caption - The caption of the link (appears beneath the link name)
  • description - The description of the link (appears beneath the link caption)
  • picture - The URL of a picture attached to this post. The picture must be at least 200px by 200px
  • link - The link attached to this post
  • properties - The key/value pairs which will appear in the stream attachment beneath the description
  • actions - One action link which will appear next to the 'Comment' and 'Like' link under posts
// create publish listener
OnPublishListener onPublishListener = new SimpleFacebook.OnPublishListener()
{

    @Override
    public void onFail(String reason)
    {
        // insure that you are logged in before publishing
        Log.w(TAG, reason);
    }

    @Override
    public void onException(Throwable throwable)
    {
        Log.e(TAG, "Bad thing happened", throwable);
    }

    @Override
    public void onThinking()
    {
        // show progress bar or something to the user while publishing
        Log.i(TAG, "In progress");
    }

    @Override
    public void onComplete(String postId)
    {
        Log.i(TAG, "Published successfully. The new post id = " + postId);
    }
};

// build feed
Feed feed = new Feed.Builder()
    .setMessage("Clone it out...")
    .setName("Simple Facebook for Android")
    .setCaption("Code less, do the same.")
    .setDescription("The Simple Facebook library project makes the life much easier by coding less code for being able to login, publish feeds and open graph stories, invite friends and more.")
    .setPicture("https://raw.github.com/sromku/android-simple-facebook/master/Refs/android_facebook_sdk_logo.png")
    .setLink("https://github.com/sromku/android-simple-facebook")
    .build();
      
 // click on button and publish
   mButtonPublishFeed.setOnClickListener(new View.OnClickListener() {
   @Override
   public void onClick(View v) {
                          mSimpleFacebook.publish(feed, onPublishListener);
   }
  });


For more detail implementation check Demo 

Enjoy.

Thanks.






No comments:

Post a Comment