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:
And add Internet Permission to the AndroidManifest.xml:
My strings.xml looks like this:
3. Step
Create an abstract class, for the Facebook connection. Don't forget to insert your app id!
4. Step
Add the connection activity to the manifest:
5. Step
Change the Main activity like that:
And see the result:
In the next part, I'll show you, how to get additional Facebook datas!
VIA : helloandroid.com
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>
- <uses-permission android:name="android.permission.INTER
NET"> - uses-permission>
- xml version="1.0" encoding="utf-8"?>
- <resources>
- <string name="welcome">Welcome string>
- <string name="app_name">FacebookTeststring>
- <string name="enter">Log in to Facebookstring>
- resources>
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);
- }
- }
Add the connection activity to the manifest:
- <activity android:name=".FBConnectionActivity&qu
ot; android:label="@string/app_name">activity>
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);
- }
- });
- }
- }
VIA : helloandroid.com