Tuesday, June 25, 2013

Easy Facebook Android SDK Simple Tutorial

Hi there! Welcome to my latest tutorial. This time I will show you how to use one of useful SDK for Android Development. It's Easy Facebook SDK. So, why do I like to use it? Why don't we use official Official Facebook SDK? The answer is simple. Because this SDK is easier than other Facebook Android SDKs. This is what Easy Facebook Android SDK developer says:

Those who tried to integrate facebook features into his own Android application knows that it's not an easy thing to do. We tested many libraries: FbRocket, FbConnect and even facebook's official sdk, but their implementation was very complicated or they had very few features. That's why we decided to create our own library from scratch.

I tested it myself and found out that this SDK is very easy to use. So, it'll save your time for development. In this tutorial, I'll show how to make a simple "Hello, world" status update via your facebook app for android. Here we go!

Step 1: Prepare your Facebook App
1. You must have a Facebook App. If you don't have any, you can create it by visiting this link
2. Press Create New App button and give a name to your app. Then, check if you're agree to Facebook Platform Policies and press Continue button.


3. Next, your App's basic setting page. Save your App ID, because we'll use it later.


4. Generate your app's signature. You need to do this because Facebook requires this for security. To do that you just need to type this command in your console or terminal:
keytool -exportcert -alias androiddebugkey -keystore ~/.android/debug.keystore | openssl sha1 -binary | openssl base64
Make sure that you have provided the correct path to the debug.keystore. For Windows, it is generally at C:\Users\<user>\.android\ and for Mac at /Users/<user>/.android/
For more information about app's signature, visit this linkhttps://developers.facebook.com/docs/mobile/android/build/#sig
Note: If you're a windows user just like me, you need to download openssl for windows. Then, add openssl.exe andJDK path to Environment Variables.
5. Copy and paste your key hash to your Facebook App Settings. You can look the location to put the key hash from the picture below.


Step 2: Create Your Facebook Android App
1. Download the latest Easy Facebook Android SDK from this link:
2. Open Eclipse and create new Android application. I use Android 2.2 as the target device.
3. Extract your download Easy Facebook Android SDK. Add new folder to your project, name it lib. Drag and drop your easyfacebookandroidsdk_x.x.jar file from your extracted file to lib folder inside your project. Add it to Build Path.

4. Open your AndroidManifest.xml file and add INTERNET permission to it

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="net.junian.kodefun.fbapp"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk android:minSdkVersion="8" />

    <application
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name" >
        <activity
            android:label="@string/app_name"
            android:name=".KodeFunFBAppActivity" >
            <intent-filter >
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>
    <uses-permission android:name="android.permission.INTERNET"/>

</manifest>

5. Modify your KodeFunFBAppActivity.java so it became like this code:
package net.junian.kodefun.fbapp;

import com.easy.facebook.android.apicall.GraphApi;
import com.easy.facebook.android.data.User;
import com.easy.facebook.android.error.EasyFacebookError;
import com.easy.facebook.android.facebook.FBLoginManager;
import com.easy.facebook.android.facebook.Facebook;
import com.easy.facebook.android.facebook.LoginListener;

import android.app.Activity;
import android.os.Bundle;
import android.util.Log;

public class KodeFunFBAppActivity extends Activity implements LoginListener {
 /** Called when the activity is first created. */

 private FBLoginManager fbLoginManager;

 //replace it with your own Facebook App ID
 public final String KODEFUNFBAPP_ID = "303121969700700";

 @Override
 public void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  //setContentView(R.layout.main);
  connectToFacebook();
 }

 public void connectToFacebook(){

  //read about Facebook Permissions here:
  //http://developers.facebook.com/docs/reference/api/permissions/
  String permissions[] = {
    "user_about_me",
    "user_activities",
    "user_birthday",
    "user_checkins",
    "user_education_history",
    "user_events",
    "user_groups",
    "user_hometown",
    "user_interests",
    "user_likes",
    "user_location",
    "user_notes",
    "user_online_presence",
    "user_photo_video_tags",
    "user_photos",
    "user_relationships",
    "user_relationship_details",
    "user_religion_politics",
    "user_status",
    "user_videos",
    "user_website",
    "user_work_history",
    "email",

    "read_friendlists",
    "read_insights",
    "read_mailbox",
    "read_requests",
    "read_stream",
    "xmpp_login",
    "ads_management",
    "create_event",
    "manage_friendlists",
    "manage_notifications",
    "offline_access",
    "publish_checkins",
    "publish_stream",
    "rsvp_event",
    "sms",
    //"publish_actions",

    "manage_pages"

  };

  fbLoginManager = new FBLoginManager(this,
    R.layout.main, 
    KODEFUNFBAPP_ID, 
    permissions);

  if(fbLoginManager.existsSavedFacebook()){
   fbLoginManager.loadFacebook();
  }
  else{
   fbLoginManager.login();
  }
 }

 @Override
 protected void onActivityResult(int requestCode, int resultCode, android.content.Intent data){
  fbLoginManager.loginSuccess(data);
 }

 public void loginSuccess(Facebook facebook) {
  GraphApi graphApi = new GraphApi(facebook);

  User user = new User();

  try{
   user = graphApi.getMyAccountInfo();

   //update your status if logged in
   graphApi.setStatus("Hello, world!");
  } catch(EasyFacebookError e){
   Log.d("TAG: ", e.toString());
  }

  fbLoginManager.displayToast("Hey, " + user.getFirst_name() + "! Login success!");
 }

 public void logoutSuccess() {
  fbLoginManager.displayToast("Logout Success!");
 }

 public void loginFail() {
  fbLoginManager.displayToast("Login Epic Failed!");
 }
}

Step 3: Test Your App
1. Run your App to your device or emulator. You'll see your app something like these pictures:

 
  


1 comment: