gpt4 book ai didi

android - 从 Facebook 连接的 Android 应用程序注销

转载 作者:行者123 更新时间:2023-11-30 03:13:05 24 4
gpt4 key购买 nike

所以我按照 Androidhive tutorial 中有关使用 Facebook 登录 Android 应用程序的说明进行操作(我知道,这里使用的大多数类都已弃用)并最终将其作为我的 MainActivity

package com.divingscores;
public class MainActivity extends Activity implements View.OnClickListener {

private static String APP_ID = "779458385402636";

Intent facebookLogoutIntent = new Intent(this, LogoutActivity.class);
private Button fbLoginButton;

private Facebook facebook;
@SuppressWarnings("deprecation")
private AsyncFacebookRunner mAsyncRunner;
String FILENAME = "AndroidSSO_data";
private SharedPreferences mPrefs;


@SuppressWarnings("deprecation")
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

//facebook = new Facebook(APP_ID);
//mAsyncRunner = new AsyncFacebookRunner(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);
}

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 onFacebookError(FacebookError e) {
// TODO Auto-generated method stub

}

@Override
public void onError(DialogError e) {
// TODO Auto-generated method stub

}



});
}}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);

facebook = new Facebook(APP_ID);
mAsyncRunner = new AsyncFacebookRunner(facebook);
fbLoginButton = (Button) findViewById(R.id.fbLoginButton);
fbLoginButton.setOnClickListener(this);
return true;
}

@Override
public void onClick(View v) {

switch(v.getId()) {


case R.id.fbLoginButton:
loginToFacebook();
break;
}

}
}

一切正常,我可以登录应用程序。但问题是我保持登录状态,因为我没有指定应用程序应该做什么,模拟器没有启动。我想要能够在登录成功时启动 activity,但不确定我应该将代码放在哪里。

最佳答案

让我们从使用 session 对象在 Android 上连接 Facebook 开始。

Pre: Import the Facebook SDK to your project.

  1. 首先在您要使用 Facebook 登录的 Activity 中创建一个方法 setupFacebookConnect()

    // Add the Session status variable to your activity
    private Session.StatusCallback statusCallback = new FBSessionStatus();

    /**
    *
    * @param Bundle
    *
    */
    public void setupFacebookConnect(Bundle savedInstanceState) {
    Settings.addLoggingBehavior(LoggingBehavior.INCLUDE_ACCESS_TOKENS);

    Session session = Session.getActiveSession();
    if (session == null) {
    if (savedInstanceState != null) {
    session = Session.restoreSession(this, null, statusCallback,
    savedInstanceState);
    }
    if (session == null) {
    session = new Session(this);
    }
    Session.setActiveSession(session);
    if (session.getState().equals(SessionState.CREATED_TOKEN_LOADED)) {
    session.openForRead(new Session.OpenRequest(this)
    .setCallback(statusCallback));
    }
    }
    }
  2. 实现 onStartonStoponSavedInstanceStateonActivityResult 方法,如下所示

    @Override
    public void onStart() {
    super.onStart();
    Session.getActiveSession().addCallback(statusCallback);
    }

    @Override
    public void onStop() {
    super.onStop();
    Session.getActiveSession().removeCallback(statusCallback);
    }

    @Override
    protected void onSaveInstanceState(Bundle outState) {
    super.onSaveInstanceState(outState);
    Session session = Session.getActiveSession();
    Session.saveSession(session, outState);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    Session.getActiveSession().onActivityResult(this, requestCode,
    resultCode, data);
    }
  3. 在Activity中添加一个内部类来监听Session状态回调

    private class FBSessionStatus implements Session.StatusCallback {

    @Override
    public void call(Session session, SessionState state,
    Exception exception) {

    }
    }
  4. 添加到您的 list

    <activity android:name="com.facebook.LoginActivity" >
    </activity>

    和访问互联网的权限

    <uses-permission android:name="android.permission.INTERNET" />

    和 Facebook 应用程序 ID

    <meta-data
    android:name="com.facebook.sdk.ApplicationId"
    android:value="APP_ID" />
  5. 现在创建一个方法connectFacebook(),您可以调用它来连接到 Facebook

    public void connectFacebook() {
    Session session = Session.getActiveSession();
    if (!session.isOpened() && !session.isClosed()) {
    session.openForRead(new Session.OpenRequest(this)
    .setCallback(statusCallback));
    } else {
    Session.openActiveSession(this, true, statusCallback);
    }
    }
  6. 最后创建一个方法disConnectFacebook,当您想要断开连接并清除 session 时调用

    public void disConnectFacebook() {
    Session session = Session.getActiveSession();
    if (!session.isClosed()) {
    session.closeAndClearTokenInformation();
    }
    }
  7. 现在,一切设置就绪,从 Activity 的 onCreate() 调用 setupFacebookConnect() 方法。您可以在您想要连接或断开连接的地方调用 connectFacebook()disConnectFacebook(),例如按下按钮或在某个对话框中,您可以在任何地方调用。

Pheww !! that was long, I hope it works, I have tested the connect and disconnect.

你可以在这里看到完整的源代码https://gist.github.com/rachitrm/7978389或分支此存储库 https://github.com/rachitrm/rm-facebook-login

关于android - 从 Facebook 连接的 Android 应用程序注销,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20595632/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com