dimanche 1 mars 2015

How I can logout from Facebook in my custom android app?

I am building an android application where I am using facebook login and after facebook login I get access_token and session that are stored in shared preferences in private mode.


I am using the below facebook login code in an activity and i want the user to logout from facebook from another fragment. How do i code on the fragment so that my current logged-in account logs out from facebook.


Here is my facebook login code -



/**
* Function to login into facebook
*/
@SuppressWarnings("deprecation")
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);
// call getprofileinformation function to get user details
getProfileInformation();

Log.d("FB Sessions", "" + facebook.isSessionValid());
}

if (expires != 0) {
facebook.setAccessExpires(expires);
}

if (!facebook.isSessionValid()) {
facebook.authorize(this,
new String[]{"email", "publish_stream"},
new Facebook.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();

// call getprofileinformation function to get user
// details
getProfileInformation();

}

@Override
public void onError(DialogError error) {
// Function to handle error

}

@Override
public void onFacebookError(FacebookError fberror) {
// Function to handle Facebook errors

}

});
}
}


Help is appreciated, thanks!


Aucun commentaire:

Enregistrer un commentaire