Search This Blog

Monday 23 July 2012

Integrating Facebook in Android Apps


Quick integration of facebook on android platform



Hello Creative Android Developers,


This post is explaining the integration of facebook a powerful social media with android applications.


facebook the powerful social media provides the enrich developer APIs to integrate the facebook features in applications. You can create lots of derivative social apps from facebook and can add a social touch to your app with facebook interaction.


This post is focusing on developing the android application with facebook APIs. The facebook SDK could be download from https://github.com/facebook/facebook-android-sdk/


Once you download the facebook-android-sdk you need to create an android project to compile the facebook-android-sdk code and create a library for facebook APIs.


Now you can create your sample code by using the above library as reference in your application.


Create a simple android application project and replace the activity code as follows.


You need to register your application on facebook platform and get the application id from the platform.

package com.example.samplefacebook;

import android.os.Bundle;

import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
import android.view.MenuItem;
import android.support.v4.app.NavUtils;
import com.facebook.android.*;
import com.facebook.android.Facebook.*;

public class MainActivity extends Activity {

   
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.activity_main, menu);
        return true;
    }

    
    Facebook facebook = new Facebook("YOUR APP ID");

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        facebook.authorize(this, new DialogListener() {
            @Override
            public void onComplete(Bundle values) {}

            @Override
            public void onFacebookError(FacebookError error) {}

            @Override
            public void onError(DialogError e) {}

            @Override
            public void onCancel() {}
        });
    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);

        facebook.authorizeCallback(requestCode, resultCode, data);
    }
    
}


This is the simple code for invoking the login interface with single-sign-on (SSO) of the facebook from your android application. 


You need to add the following permission to access the facebook platform.


Add the following line in AndroidManifest.xml of the sample.

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

Now running the application we show the following output

facebook on android
Facebook - (SSO) in Android App

Now you can make your apps more social with facebook. If you like the post then please provide your feedback.


Thanks
Creative Android Apps



1 comment: