Sachin
Sachin

Reputation: 319

Android Facebook integration does not log out

I am developing a Facebook Android integration using the below code, but once I log-in through this app then it does not logout from facebook even if I logout from Facebook app or browser both from my device. So this android app can still post on my Facebook wall. How do I logout from this app if I logout from my facebook app or facebook on browser.

 package com.facebook.androidhive;

 import java.io.FileNotFoundException;
 import java.io.IOException;
 import java.net.MalformedURLException;

 import org.json.JSONException;
 import org.json.JSONObject;

 import android.app.Activity;
 import android.content.Intent;
 import android.content.SharedPreferences;
 import android.os.Bundle;
 import android.util.Log;
 import android.view.View;
 import android.widget.Button;
 import android.widget.TextView;
 import android.widget.Toast;

 import com.facebook.android.AsyncFacebookRunner;
 import com.facebook.android.AsyncFacebookRunner.RequestListener;
 import com.facebook.android.DialogError;
 import com.facebook.android.Facebook;
 import com.facebook.android.Facebook.DialogListener;
 import com.facebook.android.FacebookError;

 public class AndroidFacebookConnectActivity extends Activity {

// Your Facebook APP ID
private static String APP_ID = "APP_ID"; // Replace with your App ID

// Instance of Facebook Class
private Facebook facebook = new Facebook(APP_ID);
private AsyncFacebookRunner mAsyncRunner;
String FILENAME = "AndroidSSO_data";
private SharedPreferences mPrefs;



@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);


    mAsyncRunner = new AsyncFacebookRunner(facebook);

    loginToFacebook();


    postToWall();


}

/**
 * Function to login into facebook
 * */
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);

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

    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

                    }

                });
    }
}

@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    facebook.authorizeCallback(requestCode, resultCode, data);
}



/**
 * Function to post to facebook wall
 * */
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() {
        }
    });

}

/**
 * Function to Logout user from Facebook
 * */
public void logoutFromFacebook() {
    mAsyncRunner.logout(this, new RequestListener() {
        @Override
        public void onComplete(String response, Object state) {
            Log.d("Logout from Facebook", response);
            if (Boolean.parseBoolean(response) == true) {
                runOnUiThread(new Runnable() {

                    @Override
                    public void run() {

                    }

                });

            }
        }

        @Override
        public void onIOException(IOException e, Object state) {
        }

        @Override
        public void onFileNotFoundException(FileNotFoundException e,
                Object state) {
        }

        @Override
        public void onMalformedURLException(MalformedURLException e,
                Object state) {
        }

        @Override
        public void onFacebookError(FacebookError e, Object state) {
        }
    });
}

 }

Upvotes: 1

Views: 1336

Answers (2)

Jesse Chen
Jesse Chen

Reputation: 4928

You can't force a logout from your app if you logout of the Facebook app, at least no API that we support with our SDK.

In theory, you could find a way to attach some sort of uninstall intent or to parse logs for any messages regarding facebook logout, but it generally is not recommended as best practice to force a logout of another app when another app logs out (its not intuitive to the user that this is going to happen).

Upvotes: 1

Nguyễn Anh Quế
Nguyễn Anh Quế

Reputation: 243

try remove this code

if (access_token != null) {
        facebook.setAccessToken(access_token);

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

Upvotes: 0

Related Questions