Android facebook上的SSO

时间:2012-07-30 10:13:21

标签: android facebook single-sign-on

我在android中开发了一个facebook应用程序。我的设备有默认的android facebook应用程序。当我运行我的应用程序时,如果我输入凭据,则默认的fb也会登录。当我退出我的应用程序时,默认应用程序不会被注销。那么我怎样才能从我的应用程序中获得logout default fb。我也用过SSO。但仍有同样的问题。

我的代码:

public class SampleApp extends Activity {

// Set Application ID to your registered app client_id
// See http://www.facebook.com/developers/createapp.php
public static final String APP_ID = "APP ID";

private static final String[] PERMISSIONS = new String[] {
        "publish_stream", "read_stream", "offline_access" };
private TextView mText;
private Handler mHandler = new Handler();

private Facebook mFacebook;
private AsyncFacebookRunner mAsyncRunner;
byte[] raw;
private SharedPreferences mPrefs;

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    // Make sure the app client_app has been set
    if (APP_ID == null) {
        Util.showAlert(this, "Warning",
                "Facebook Applicaton ID must be set...");
    }

    // Initialize the content view
    setContentView(R.layout.main);
    // Get the status text line resource
    mText = (TextView) SampleApp.this.findViewById(R.id.txt);

    // Setup the ListView Adapter that is loaded when selecting
    // "get friends"

    // Define a spinner used when loading the friends over the network

    // Initialize the Facebook session
    mFacebook = new Facebook(APP_ID);
    mAsyncRunner = new AsyncFacebookRunner(mFacebook);

    mPrefs = getPreferences(MODE_PRIVATE);
    String access_token = mPrefs.getString("access_token", null);
    long expires = mPrefs.getLong("access_expires", 0);
    if (access_token != null) {
        mFacebook.setAccessToken(access_token);
    }
    if (expires != 0) {
        mFacebook.setAccessExpires(expires);
    }

}

// ////////////////////////////////////////////////////////////////////

public void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    Log.d("FB Sample App", "onActivityResult(): " + requestCode);
    mFacebook.authorizeCallback(requestCode, resultCode, data);
}



private class LogoutRequestListener implements RequestListener {

    /** Called when the request completes w/o error */
    public void onComplete(String response) {

        // Only the original owner thread can touch its views
        SampleApp.this.runOnUiThread(new Runnable() {
            public void run() {
                mText.setText("Thanks for using FB Sample App. Bye bye...");

            }
        });

        // Dispatch on its own thread
        mHandler.post(new Runnable() {
            public void run() {
            }
        });
    }



    @Override
    public void onComplete(String response, Object state) {
        // TODO Auto-generated method stub

    }

    @Override
    public void onIOException(IOException e, Object state) {
        // TODO Auto-generated method stub

    }

    @Override
    public void onFileNotFoundException(FileNotFoundException e,
            Object state) {
        // TODO Auto-generated method stub

    }

    @Override
    public void onMalformedURLException(MalformedURLException e,
            Object state) {
        // TODO Auto-generated method stub

    }

    @Override
    public void onFacebookError(FacebookError e, Object state) {
        // TODO Auto-generated method stub

    }

}


@Override
public boolean onCreateOptionsMenu(Menu menu) {
    MenuInflater inflater = getMenuInflater();
    inflater.inflate(R.menu.main_menu, menu);
    return true;
}


@Override
public boolean onPrepareOptionsMenu(Menu menu) {

    MenuItem loginItem = menu.findItem(R.id.login);

    if (mFacebook.isSessionValid()) {
        loginItem.setTitle("Logout");


    } else {
        loginItem.setTitle("Login");


    }
    loginItem.setEnabled(true);
    return super.onPrepareOptionsMenu(menu);
}


@Override
public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()) {

    // Login/logout toggle
    case R.id.login:

        if (!mFacebook.isSessionValid()) {

            mFacebook.authorize(this, new DialogListener() {
                @Override
                public void onComplete(Bundle values) {
                    Log.v("Entered ", "No ERRRRRRRRRRRR");
                    SharedPreferences.Editor editor = mPrefs.edit();
                    editor.putString("access_token",
                            mFacebook.getAccessToken());
                    editor.putLong("access_expires",
                            mFacebook.getAccessExpires());
                    editor.commit();
                    Intent i=new Intent(SampleApp.this,second.class);
                    startActivity(i);
                }

                @Override
                public void onFacebookError(FacebookError error) {
                }

                @Override
                public void onError(DialogError e) {
                }


                public void onCancel() {
                }
            });
        }else{

            mFacebook.setAccessToken(null);
            mFacebook.setAccessExpires(0);

            AsyncFacebookRunner asyncRunner = new AsyncFacebookRunner(mFacebook);
             asyncRunner.logout(this.getBaseContext(), new  LogoutRequestListener());
        }

        break;


    default:
        return false;

    }
    return true;
}

}
}

0 个答案:

没有答案