Using Facebook SDK in Android development, Part 2.
In this part, I show you an android application, which logging in to Facebook, then get the Facebook ID.
Please go through the first part of the tutorial, before reading this post.
1. Step
Create a new Android project in Eclipse, choose "create project from existing source", and set the location to: "Your Facebook SDK"\facebook. In my case it's D:\facebook android sdk\facebook.

2. Step
Create a new empty Android project, and open project properties/android. Hit Add in the Library section, add the facebook project.

3. Step
Change the main.xml like this:
- <?xml version="1.0" encoding="utf-8"?>
- <RelativeLayout xmlns:android="http://schemas.android.c
om/apk/res/android" - android:orientation="vertical" android:layout_width="fill_parent"
- android:layout_height="fill_parent">
- <TextView android:layout_width="fill_parent" android:id="@+id/textFacebook"
- android:gravity="center_horizontal" android:layout_height="wrap_content"
- android:text="@string/welcome" android:layout_alignParentTop="true" />
- <Button android:text="@string/enter" android:id="@+id/buttonLogin"
- android:layout_below="@+id/textFacebook"
- android:layout_centerHorizontal="true" android:layout_width="wrap_content"
- android:layout_height="wrap_content" android:layout_marginTop="30dip"></Button>
- <ProgressBar android:id="@+id/progressLogin"
- android:layout_centerInParent="true" android:layout_width="wrap_content"
- android:visibility="gone" android:layout_height="wrap_content"></ProgressBar>
- </RelativeLayout>
And add Internet Permission to the AndroidManifest.xml:
- <uses-permission android:name="android.permission.INTER
NET"> - </uses-permission>
My strings.xml looks like this:
- <?xml version="1.0" encoding="utf-8"?>
- <resources>
- <string name="welcome">Welcome </string>
- <string name="app_name">FacebookTest</string>
- <string name="enter">Log in to Facebook</string>
- </resources>
3. Step
Create an abstract class, for the Facebook connection. Don't forget to insert your app id!
- public abstract class FBConnectionActivity extends Activity {
- private Facebook mFacebook;
- private AsyncFacebookRunner mAsyncRunner;
- private SharedPreferences sharedPrefs;
- private TextView username;
- private ProgressBar pb;
- public void setConnection() {
- mContext = this;
- mFacebook = new Facebook(APP_ID);
- mAsyncRunner = new AsyncFacebookRunner(mFacebook);
- }
- public void getID(TextView txtUserName, ProgressBar progbar) {
- username = txtUserName;
- pb = progbar;
- if (isSession()) {
- Log.d(TAG, "sessionValid");
- mAsyncRunner.request("me", new IDRequestListener());
- } else {
- // no logged in, so relogin
- Log.d(TAG, "sessionNOTValid, relogin");
- mFacebook.authorize(this, PERMS, new LoginDialogListener());
- }
- }
- public boolean isSession() {
- sharedPrefs = PreferenceManager.getDefaultSharedPreferences(mContext);
- Log.d(TAG, access_token);
- if (access_token != null && expires != -1) {
- mFacebook.setAccessToken(access_token);
- mFacebook.setAccessExpires(expires);
- }
- return mFacebook.isSessionValid();
- }
- private class LoginDialogListener implements DialogListener {
- @Override
- public void onComplete(Bundle values) {
- Log.d(TAG, "LoginONComplete");
- long token_expires = mFacebook.getAccessExpires();
- Log.d(TAG, "AccessToken: " + token);
- Log.d(TAG, "AccessExpires: " + token_expires);
- sharedPrefs = PreferenceManager
- .getDefaultSharedPreferences(mContext);
- sharedPrefs.edit().putLong("access_expires", token_expires)
- .commit();
- sharedPrefs.edit().putString("access_token", token).commit();
- mAsyncRunner.request("me", new IDRequestListener());
- }
- @Override
- public void onFacebookError(FacebookError e) {
- Log.d(TAG, "FacebookError: " + e.getMessage());
- }
- @Override
- public void onError(DialogError e) {
- Log.d(TAG, "Error: " + e.getMessage());
- }
- @Override
- public void onCancel() {
- Log.d(TAG, "OnCancel");
- }
- }
- private class IDRequestListener implements RequestListener {
- @Override
- try {
- Log.d(TAG, "IDRequestONComplete"
;); - Log.d(TAG, "Response: " + response.toString());
- public void run() {
- username.setText("Welcome: " + name+"\n ID: "+id);
- pb.setVisibility(ProgressBar.GONE);
- }
- });
- } catch (JSONException e) {
- Log.d(TAG, "JSONException: " + e.getMessage());
- } catch (FacebookError e) {
- Log.d(TAG, "FacebookError: " + e.getMessage());
- }
- }
- @Override
- Log.d(TAG, "IOException: " + e.getMessage());
- }
- @Override
- Log.d(TAG, "FileNotFoundException: " + e.getMessage());
- }
- @Override
- Log.d(TAG, "MalformedURLException: " + e.getMessage());
- }
- @Override
- Log.d(TAG, "FacebookError: " + e.getMessage());
- }
- }
- @Override
- protected void onActivityResult(int requestCode, int resultCode, Intent data) {
- mFacebook.authorizeCallback(requestCode, resultCode, data);
- }
- }
4. Step
Add the connection activity to the manifest:
- <activity android:name=".FBConnectionActivity&qu
ot; android:label="@string/app_name"></activity>
5. Step
Change the Main activity like that:
- public class Main extends FBConnectionActivity {
- private TextView txtUserName;
- private ProgressBar pbLogin;
- @Override
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.main);
- txtUserName = (TextView) findViewById(R.id.textFacebook);
- pbLogin = (ProgressBar) findViewById(R.id.progressLogin);
- btnLogin.setOnClickListener(new OnClickListener() {
- @Override
- pbLogin.setVisibility(ProgressBar.VISIBLE);
- setConnection();
- getID(txtUserName, pbLogin);
- }
- });
- }
- }
And see the result:

In the next part, I'll show you, how to get additional Facebook datas!
New tutorials from Helloandroid
Recent Apps
Android on Twitter
-
@Idevicegazette (iDevice Gazette)
GSM-to-Skype bridge lets you lose those roaming fees http://bit.ly/lbRJeh #android
11 years 6 weeks ago -
@tommy_banane (tom b.)
RT @AndroidFavorite: #Android New Desktop Android Market Is Live, Adds Several New Features http://zorr0.nl/lFwXNz
11 years 6 weeks ago -
@dwilliams5 (Dennis Williams)
just completed a runtastic run of 3.02 km in 40 min 11 s with #runtastic #Android App: http://tinyurl.com/5tvrpe3
11 years 6 weeks ago -
@S_Pinz (Spinz!)
RT @Androidheadline: Out of box #LG Optimus 3D got Quadrant 2420 score. Thanks @blink_c #io2011 #android http://twitpic.com/4whkdz
11 years 6 weeks ago -
@tayaitapps (Taya IT)
Next Google TV Looks A Lot Like Android http://t.co/dvlTim3 via @alleyinsider #google #apple #android #tv #honeycomb
11 years 6 weeks ago
Poll
Useful resources
Android Development Projects
- iOS/Android Developer to take older Games and bring them Current
- Android apps developer - need to finish urgent.
- Buliding MobileApp For onlie order
- looking for android APP developers
- Create an ecommerce app
- text-to voice for smartphones IOS - GOOGLE - HARMONY - AND ALEXA
- Optimize Images on App
- Create small feature with drag-drop text for Android
- Scouting for advanced website and Mobile apps developers. Potential Long-term contract.
- BLACK SCREEN