Facebook Integration in android

Introduction:
   Now I am going to share about the Facebook Integration without using external Facebook SDK Library.Its a better way to use to Facebook in our project.Because some time Facebook SDK Library methods files can able to deprecate in future, So this way is best to integrate Facebook in android.

Also get facebook profile picture,user name,first name,last name,email and fb id.

demo





Using this code

1. Generating Key Hash for Facebook

Open your command prompt (CMD) and run the following command to generate your keyhash. While generating hashkey it should ask you password. Give password as android. If it don’t ask for password your keystore path is incorrect. 

keytool -exportcert -alias androiddebugkey -keystore "C:\Users\User\.android\debug.keystore" | openssl sha1 -binary | openssl base64

- You will get your hash key for debug.keystore.

Example-  XQQl+LMlyirxGOVWiJ34L1XRqPg=


2. Registering your Facebook application
   
    i).Open Facebook developer site
   ii).App-Add a New App-Choose Android icon-Type App Name-Application category.
  iii).Enter Android application package name and Activity name.
       

 iii).Then Enter Key Hash value.



iv).Click Create App,Finally get APP ID.



3.Add Facebook SDK codes in our package

i).Add fb.android package in your project then only able to access facebook.

                                   




activity_main.xml

<RelativeLayout
 xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/ba" >

<Button
 android:id="@+id/button1"
 android:layout_width="wrap_content"
 android:layout_height="50dp"
 android:layout_alignParentTop="true"
 android:layout_margin="15dp"
 android:background="@drawable/login" />

<Button
 android:id="@+id/logout"
 android:layout_width="wrap_content"
 android:layout_height="50dp"
 android:layout_alignParentBottom="true"
 android:layout_centerInParent="true"
 android:layout_margin="15dp"
 android:background="@drawable/logout" />

<RelativeLayout
 android:id="@+id/layout"
 android:layout_width="match_parent"
 android:layout_height="match_parent"
 android:layout_above="@+id/logout"
 android:layout_below="@+id/button1"
 android:layout_marginLeft="5dp"
 android:layout_marginRight="5dp"
 android:visibility="visible" >

<ImageView
 android:id="@+id/fb_profile_photo"
 android:layout_width="100dp"
 android:layout_height="100dp"
 android:layout_alignParentTop="true"
 android:layout_centerInParent="true"
 android:layout_margin="5dp" />

<LinearLayout
 android:id="@+id/layout1"
 android:layout_width="match_parent"
 android:layout_height="wrap_content"
 android:layout_below="@+id/fb_profile_photo"
 android:layout_marginTop="10dp"
 android:orientation="horizontal"
 android:padding="5dp"
 android:weightSum="2" >

<TextView
 android:id="@+id/name11"
 android:layout_width="match_parent"
 android:layout_height="wrap_content"
 android:layout_weight="0.55"
 android:text="User Name"
 android:textSize="15dp"
 android:textStyle="bold" />

<TextView
 android:layout_width="match_parent"
 android:layout_height="wrap_content"
 android:layout_weight=".95"
 android:text=":"
 android:textSize="15dp"
 android:textStyle="bold" />

<TextView
 android:id="@+id/username"
 android:layout_width="match_parent"
 android:layout_height="wrap_content"
 android:layout_weight="0.5"
 android:textSize="15dp" />
</LinearLayout>

<LinearLayout
 android:id="@+id/layout2"
 android:layout_width="match_parent"
 android:layout_height="wrap_content"
 android:layout_below="@+id/layout1"
 android:orientation="horizontal"
 android:padding="5dp"
 android:weightSum="2" >

<TextView
 android:id="@+id/emaill"
 android:layout_width="match_parent"
 android:layout_height="wrap_content"
 android:layout_weight="0.55"
 android:text="First Name"
 android:textSize="15dp"
 android:textStyle="bold" />

<TextView
 android:layout_width="match_parent"
 android:layout_height="wrap_content"
 android:layout_weight=".95"
 android:text=":"
 android:textSize="15dp"
 android:textStyle="bold" />

<TextView
 android:id="@+id/fname"
 android:layout_width="match_parent"
 android:layout_height="wrap_content"
 android:layout_weight="0.5"
 android:textSize="15dp" />
</LinearLayout>

<LinearLayout
 android:id="@+id/layout3"
 android:layout_width="match_parent"
 android:layout_height="wrap_content"
 android:layout_below="@+id/layout2"
 android:orientation="horizontal"
 android:padding="5dp"
 android:weightSum="2" >

<TextView
 android:id="@+id/firstname"
 android:layout_width="match_parent"
 android:layout_height="wrap_content"
 android:layout_weight="0.55"
 android:text="Last Name"
 android:textSize="15dp"
 android:textStyle="bold" />

<TextView
 android:layout_width="match_parent"
 android:layout_height="wrap_content"
 android:layout_weight=".95"
 android:text=":"
 android:textSize="15dp"
 android:textStyle="bold" />

<TextView
android:id="@+id/lname"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="0.5"
android:textSize="15dp" />
</LinearLayout>

<LinearLayout
 android:id="@+id/layout4"
 android:layout_width="match_parent"
 android:layout_height="wrap_content"
 android:layout_below="@+id/layout3"
 android:orientation="horizontal"
 android:padding="5dp"
 android:weightSum="2" >

<TextView
 android:id="@+id/lastname"
 android:layout_width="match_parent"
 android:layout_height="wrap_content"
 android:layout_weight="0.55"
 android:text="Email"
 android:textSize="15dp"
 android:textStyle="bold" />

<TextView
 android:layout_width="match_parent"
 android:layout_height="wrap_content"
 android:layout_weight=".95"
 android:text=":"
 android:textSize="15dp"
 android:textStyle="bold" />

<TextView
 android:id="@+id/email"
 android:layout_width="match_parent"
 android:layout_height="wrap_content"
 android:layout_weight="0.5"
 android:textSize="15dp" />
</LinearLayout>

<LinearLayout
 android:id="@+id/layout5"
 android:layout_width="match_parent"
 android:layout_height="wrap_content"
 android:layout_below="@+id/layout4"
 android:orientation="horizontal"
 android:padding="5dp"
 android:weightSum="2" >

<TextView
 android:id="@+id/fb"
 android:layout_width="match_parent"
 android:layout_height="wrap_content"
 android:layout_weight="0.55"
 android:text="FB Id"
 android:textSize="15dp"
 android:textStyle="bold" />

<TextView
 android:layout_width="match_parent"
 android:layout_height="wrap_content"
 android:layout_weight=".95"
 android:text=":"
 android:textSize="15dp"
 android:textStyle="bold" />

<TextView
 android:id="@+id/fbid"
 android:layout_width="match_parent"
 android:layout_height="wrap_content"
 android:layout_weight="0.5"
 android:textSize="15dp" />
</LinearLayout>
</RelativeLayout>


</RelativeLayout>

MainActivity.java
package com.example.facebook_integration_android;

import java.net.HttpURLConnection;
import java.net.URL;
import org.json.JSONObject;
import org.json.JSONTokener;
import fb.android.DialogError;
import fb.android.Facebook;
import fb.android.FacebookError;
import fb.android.SessionStore;
import fb.android.Facebook.DialogListener;
import android.app.Activity;
import android.app.ProgressDialog;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;


public class MainActivity extends Activity {

Button fb_btn,fb_logout;
Facebook mFacebook;
private static final String[] PERMISSIONS = new String[] {
"publish_stream""read_stream""offline_access""email" };
private static final String APP_ID = "1392931717639402";
ProgressDialog dialog;
String fb_id,name,mfbemail,mfirstname,mlastname,access_token,access_expires,musername;
Bitmap fbpicture;
TextView username,fname,lname,email,fbid;
ImageView fb_profile_photo;
RelativeLayout layout;
int what = 1;

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

fb_btn=(Button)findViewById(R.id.button1);
fb_logout=(Button)findViewById(R.id.logout);
username=(TextView)findViewById(R.id.username);
fname=(TextView)findViewById(R.id.fname);
lname=(TextView)findViewById(R.id.lname);
email=(TextView)findViewById(R.id.email);
fbid=(TextView)findViewById(R.id.fbid);
fb_profile_photo=(ImageView)findViewById(R.id.fb_profile_photo);
layout=(RelativeLayout)findViewById(R.id.layout);
layout.setVisibility(View.INVISIBLE);
fb_logout.setVisibility(View.INVISIBLE);

mFacebook = new Facebook(APP_ID);
fb_btn.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View v) 
 {
// TODO Auto-generated method stub
  onFacebookClick();
 }
});

fb_logout.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View v) {
// TODO Auto-generated method stub
    fbLogout();
  }
});

}

private void onFacebookClick()
{
    mFacebook.authorize(thisPERMISSIONS, -1,new FbLoginDialogListener());
}
private final class FbLoginDialogListener implements DialogListener {
public void onComplete(Bundle values) 
{
    fb_btn.setClickable(true);
    getFbName();
}

public void onFacebookError(FacebookError error) {

}

public void onError(DialogError error) {

}

public void onCancel() {
}
}

private void getFbName()
{

new Thread() {
@Override
public void run() {

int what = 1;

try {
  String me = mFacebook.request("me");
  Log.e("meeeeeeeeee", me);

  JSONObject jsonObj = (JSONObject) new JSONTokener(me)
  .nextValue();

  fb_id=jsonObj.getString("id");
  name = jsonObj.getString("name");
  mfbemail = jsonObj.getString("email");
  mfirstname=jsonObj.getString("first_name");
  mlastname=jsonObj.getString("last_name");
  String user_id=fb_id;

  access_token=mFacebook.getAccessToken();

  Log.d("facebook_imageid",user_id);
  URL url = new URL("https://graph.facebook.com/"+user_id+"/picture?type=large");
  HttpURLConnection conn = (HttpURLConnection) url.openConnection();
  HttpURLConnection.setFollowRedirects(true);
  conn.setInstanceFollowRedirects(true);
  fbpicture = BitmapFactory.decodeStream(conn.getInputStream());

  access_token = mFacebook.getAccessToken();

  Log.e("response", jsonObj.toString());
  Log.e("email"mfbemail);
  Log.e("fb_id"fb_id);
  Log.e("name",name);
  Log.e("first_name"mfirstname);
  Log.e("last_name",mlastname);


  setText(username,name);
  setText(fname,mfirstname);
  setText(lname,mlastname);
  setText(email,mfbemail);
  setText(fbid,fb_id);

  fb_profile_photo.setImageBitmap(fbpicture);


  } catch (Exception ex) {
ex.printStackTrace();
}
}

}.start();
layout.setVisibility(View.VISIBLE);
fb_logout.setVisibility(View.VISIBLE);
fb_btn.setVisibility(View.INVISIBLE);
}
private void setText(final TextView text,final String value){
runOnUiThread(new Runnable() {
@Override
public void run() 
 {
   text.setText(value);
 }
});
}

private void fbLogout() {

new Thread() {
@Override
public void run()
 {
    SessionStore.clear(MainActivity.this);
 try 
 {
   what = 1;
   mFacebook.logout(MainActivity.this);
catch (Exception ex) {
ex.printStackTrace();
}

mHandler.sendMessage(mHandler.obtainMessage(what));
}
}.start();
}

private Handler mHandler = new Handler() {
@Override
public void handleMessage(Message msg) {
//mProgress.dismiss();

if (what == 1)
{
 layout.setVisibility(View.INVISIBLE);
 fb_logout.setVisibility(View.INVISIBLE);
 fb_btn.setVisibility(View.VISIBLE);
 Toast.makeText(getApplicationContext(), "Successfully loged out from your facebook account",  Toast.LENGTH_LONG).show();
else {

    }
  }
 };
}



                                       


Previous
Next Post »

1 comments:

Write comments
Anonymous
AUTHOR
February 17, 2015 at 12:57 PM delete

Nice and helpfull tutorial

Reply
avatar