Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

Android Login with Facebook

DZone's Guide to

Android Login with Facebook

· Java Zone
Free Resource

Bitbucket is for the code that takes us to Mars, decodes the human genome, or drives your next car. What will your code do? Get started with Bitbucket today, it's free.


Pre-requisite

Official Document: https://developers.facebook.com/docs/android/

Official Tutorial: https://developers.facebook.com/docs/android/login-with-facebook/v2.2

Previous Article: http://www.kpbird.com/2013/03/android-login-using-facebook-sdk-30.html

1. Facebook Android SDK 3.0 -Download

2. Facebook API Key from Facebook Application - Detail

3. Hash Key of your debug certificate  - Detail


Usage

Step 1: Import "FacebookLoginUtils.java" class in your package

Step 2: Implement callback listener

public class MyActivity extends Activity implements FacebookLoginUtils.FacebookLoginStatus 

Step 3: Declare callback method

@Override
    public void OnSuccessFacebookLogin(Bundle profile) {
        Log.i(TAG,profile.getString(FacebookLoginUtils.USERID));
        Log.i(TAG,profile.getString(FacebookLoginUtils.EMAIL));
        Log.i(TAG,profile.getString(FacebookLoginUtils.NAME));
        Log.i(TAG,profile.getString(FacebookLoginUtils.FIRST_NAME));
        Log.i(TAG,profile.getString(FacebookLoginUtils.LAST_NAME));

    }

Step 4: Declare object of FacebookLoginUtils.java

private FacebookLoginUtils fLogin;

Step 5: Initialize object in onCreate method

fLogin = new FacebookLoginUtils(this, R.id.activity_login_fb);
        fLogin.setLoginStatus(this);
        fLogin.setEnable(true);

Step 6: Declare few supporting methods

protected void onResume() {
        super.onResume();
        fLogin.onResume();
    };

    @Override
    protected void onPause() {
        super.onPause();
        fLogin.onPause();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        fLogin.onDestroy();
    }


    @Override
    protected void onActivityResult(int requestCode, int responseCode,Intent intent) {
        fLogin.onActivityResult(requestCode, responseCode, intent);
    }

FAQ

Q: What is the problem of above approach ?

A: Your activity has a lot of scattered code for Facebook login. It will be more complex and unmanageable when you will have another social network login, like Facebook and Twitter.


Q: How I will receive a response for login success?

A: You need to implement interface "FacebookLoginStatus", on successful login you will receive callback "OnSuccessFacebookLogin"


Q: What will be received on login success ?

A: You will have Name, Email , First Name, Last Name, User Id.


Q: Do I need to handle login failure/error ?

A: No, FacebookLoginUtils.java will handle login failure, error and display appropriate message on screen. 

Bitbucket is the Git solution for professional teams who code with a purpose, not just as a hobby. Get started today, it's free.

Topics:

Published at DZone with permission of Ketan Parmar, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

THE DZONE NEWSLETTER

Dev Resources & Solutions Straight to Your Inbox

Thanks for subscribing!

Awesome! Check your inbox to verify your email so you can start receiving the latest in tech news and resources.

X

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}