Wednesday, April 25, 2012

Generating App Signature for Facebook Settings --2


Now our project setup is done. We can start coding our facebook application.
3. Open your AndroidManifest.xml file add network connect permission in order to connect to internet.
<uses-permission android:name="android.permission.INTERNET"/>
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
    package="com.facebook.androidhive"
    android:versionCode="1"
    android:versionName="1.0" >
 
    <uses-sdk android:minSdkVersion="8" />
 
    <application
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name" >
        <activity
            android:label="@string/app_name"
            android:name=".AndroidFacebookConnectActivity" >
            <intent-filter >
                <action android:name="android.intent.action.MAIN" />
 
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>
 
    <!-- Connect to Internet Permissions -->
    <uses-permission android:name="android.permission.INTERNET"/>
 
</manifest>
4. Open Your Main Activity Class and initialize all the variables needed.
public class AndroidFacebookConnectActivity extends Activity {
 
    // Your Facebook APP ID
    private static String APP_ID = "308180782571605"; // Replace your App ID here
 
    // Instance of Facebook Class
    private Facebook facebook;
    private AsyncFacebookRunner mAsyncRunner;
    String FILENAME = "AndroidSSO_data";
    private SharedPreferences mPrefs;
 
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
 
        facebook = new Facebook(APP_ID);
        mAsyncRunner = new AsyncFacebookRunner(facebook);
5. I created a simple interface which contains button to login, post to wall, show access tokens and logout for testing purpose.

Login to Facebook Account

I used a button to login into facebook account. In your activity write a click event for Login button click. Inside click event declare a function named loginToFacebook();
Login button click event
btnFbLogin.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
            loginToFacebook();
        }
});
and function body for loginToFacebook() function is:
public void loginToFacebook() {
    mPrefs = getPreferences(MODE_PRIVATE);
    String access_token = mPrefs.getString("access_token", null);
    long expires = mPrefs.getLong("access_expires", 0);
 
    if (access_token != null) {
        facebook.setAccessToken(access_token);
    }
 
    if (expires != 0) {
        facebook.setAccessExpires(expires);
    }
 
    if (!facebook.isSessionValid()) {
        facebook.authorize(this,
                new String[] { "email", "publish_stream" },
                new DialogListener() {
 
                    @Override
                    public void onCancel() {
                        // Function to handle cancel event
                    }
 
                    @Override
                    public void onComplete(Bundle values) {
                        // Function to handle complete event
                        // Edit Preferences and update facebook acess_token
                        SharedPreferences.Editor editor = mPrefs.edit();
                        editor.putString("access_token",
                                facebook.getAccessToken());
                        editor.putLong("access_expires",
                                facebook.getAccessExpires());
                        editor.commit();
                    }
 
                    @Override
                    public void onError(DialogError error) {
                        // Function to handle error
 
                    }
 
                    @Override
                    public void onFacebookError(FacebookError fberror) {
                        // Function to handle Facebook errors
 
                    }
 
                });
    }
}
android facebook login

Posting Message to Facebook Wall

write a click event for post to wall button and inside click event write a function namedpostToWall()
btnPostToWall.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        postToWall();
    }
});
and function body for postToWall() function is:
public void postToWall() {
    // post on user's wall.
    facebook.dialog(this, "feed", new DialogListener() {
 
        @Override
        public void onFacebookError(FacebookError e) {
        }
 
        @Override
        public void onError(DialogError e) {
        }
 
        @Override
        public void onComplete(Bundle values) {
        }
 
        @Override
        public void onCancel() {
        }
    });
 
}
android facebook posting to wall
android facebook posting to wall


No comments: