1

我正在使用以下代码开发 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) {
        }
    });
}

 }
4

2 回答 2

1

如果您注销 Facebook 应用程序,则不能强制从您的应用程序注销,至少我们的 SDK 不支持任何 API。

理论上,您可以找到一种方法来附加某种卸载意图或解析有关 facebook 注销的任何消息的日志,但通常不建议将另一个应用程序注销时强制注销另一个应用程序作为最佳实践(它不是用户直观地知道这将发生)。

于 2012-12-03T01:02:21.723 回答
0

尝试删除此代码

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

        Log.d("FB Sessions", "" + facebook.isSessionValid());
    }
于 2012-12-01T16:21:50.293 回答