Saturday, September 29, 2012

Facebook Integration in Android

Facebook Integration in Android

Now FB integration is very easy and any Android developer make Android app or integrate in 30mins.
Now we start integration.
Step1:

Register your app on developer.facebook.com.
Now here you get the APP ID like 554684564652.

Step2:

Install Facebook SDK for Android from https://github.com/facebook/facebook-android-sdk.
















Now after complete downloading extract file and you get three folders now import only folder with name facebook as shown in below picture.

Now import in your IDE.

Step3:

Now create Android Project.


After creating project add Facebook library as a reference in our My Facebook Project.


Now complete the Process.


Step4:

Now we are starting code and it is very easy.
before starting code i give you simple trick how you make FB integration in android.
There are only two thnigs which we use in making FB in Android.
1. RequestListener
2. DialogListener

When you want to show something like you want to login in FB. Now we use here DialogListener its mean here FB login dialog create. Same if we want to share any thing from our app on FB we again use DialogListenr. Its mean when we want to send some thing on Facebook we use DialogListener.

Now when we want to get something from Facebook we use RequestListenr, like i want to get my FriendList now i request to Facebook please send me my FriendList using RequestListener.

Now we complete our app in this manner.
Now first i start to make login and according to our above disscussion i need some dialog to send information to facebook i use DialogListener.
One more thing when i use DialogListener i use Facebook Object. And when i send some request to facebook using RequestListenr i use AsyncFacebookRunner.

My Facebook App Interface:


Java Code:


FaceBookLoginDialog class:

Now run App and Press Login button:
Here when u press login there is a chance you see the white screen only as shown below.
This happen when a device install the latest update of Facebook App and already login. Now we handle this situation by changing in facebook library.
Open facebook library and open Facebook.java and here find constant
" private static final int DEFAULT_AUTH_ACTIVITY_CODE = 32665;"
and change its value to -1.
After changing it look like.
" private static final int DEFAULT_AUTH_ACTIVITY_CODE = -1;"


After changing the code projet clean or if in Facebook.java show you the error no problem now close the Facebook.Java file and work your orignal Project and leave the error or if show.

Now again install App in your device and press Login.

Now i login and i got the response from Facebook is.

Next we logout:
Here we not require any dialog for logout. Its mean i only request to Facebook please logout me.
So i here we use Request listener.


After pressing Logout Button it give you response.
Now in same manner i am geting the friends list.
Which is in the form of JSON.




You can download complete code from
http://code.google.com/p/androidfacebook/downloads/list