Android Facebook集成不会注销

时间:2012-12-01 13:41:53

标签: android facebook facebook-login facebook-android-sdk

我正在使用以下代码开发Facebook Android集成,但是一旦我通过此应用程序登录,即使我从Facebook设备或浏览器从我的设备注销,它也不会从Facebook注销。所以这个Android应用程序仍然可以发布在我的Facebook墙上。如果我从浏览器上的facebook应用程序或Facebook注销,如何从此应用程序注销。

 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) {
        }
    });
}

 }

2 个答案:

答案 0 :(得分:1)

如果您退出Facebook应用程序,则无法强制从您的应用程序注销,至少没有我们支持SDK的API。

理论上,您可以找到一种方法来附加某种卸载意图或解析有关Facebook注销的任何消息的日志,但通常不建议在另一个应用程序注销时强制注销另一个应用程序作为最佳做法(对于用户而言,这不会直观)。

答案 1 :(得分:0)

尝试删除此代码

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

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