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
-
@StephanieNich10 (Stephanie Nichols)#android I laughed so hard at ochocinco 's avi. Hahahaha http://t.co/MPuhhi3m
25 weeks 4 days ago -
@CarlaAtkins8 (Carla Atkins)#android Omg! This is actually f'n interesting http://t.co/JodMOehr
25 weeks 4 days ago -
@MarianMcleod12 (Marian Mcleod)#android Precisely what song is? http://t.co/YmJXU0rB
25 weeks 4 days ago -
@JoBeach15 (Jo Beach)#android haha this made me laugh, i love ted:-) http://t.co/gtcWQ79C
25 weeks 4 days ago -
@aochart3 (青ちゃ)Start playing Paradise Island on Android http://t.co/DEID0Ao5 #Android #Androidgames #Gameinsight http://t.co/e1bifSeL
25 weeks 4 days ago
Poll
Useful resources
Android Development Projects
- Android bases applications development (only indian & very skilled developers apply) by sudhirjeet01
- Health Care App - want freelancer in Georgia by rshearer
- IOS+Android+HTML5+PHP Expert Needed Urgently by johnusa1
- NFC Keyboard Wedge by billinginfoes
- I need an iphone and android app + web services by senjy
- Mobile Remote control software for android only by leoagent786
- Add a "Print" button, with code, to our existing process (PDF explains) by sportingchance
- I need a phone app - Android and Iphone - a copy of an existing app by DooniseP
- Apple & Android app - Connecting our CRM and an external GPS device by kshyamnatraj
- Flash&Actionscript app for android by mobistar2013



