我正在尝试将 Facebook 登录信息集成到我的应用程序中。用户使用 Facebook 登录,然后应用程序获取用户电子邮件地址,该电子邮件地址被传递到我的应用程序登录系统(帐户存储在远程 mysql 数据库中)。我有以下代码:
public class FbLoginActivity extends Activity {
private static List<String> permissions;
Session.StatusCallback statusCallback = new SessionStatusCallback();
ProgressDialog dialog;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.fblogin);
Button fbButton = (Button) findViewById(R.id.fbshare);
/***** FB Permissions *****/
permissions = new ArrayList<String>();
permissions.add("email");
/***** End FB Permissions *****/
fbButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Check if there is any Active Session, otherwise Open New
// Session
Session session = Session.getActiveSession();
if (!session.isOpened()) {
session.openForRead(new Session.OpenRequest(
FbLoginActivity.this).setCallback(statusCallback)
.setPermissions(permissions));
} else {
Session.openActiveSession(FbLoginActivity.this, true,
statusCallback);
}
}
});
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);
session.addCallback(statusCallback);
if (session.getState().equals(SessionState.CREATED_TOKEN_LOADED)) {
session.openForRead(new Session.OpenRequest(this).setCallback(
statusCallback).setPermissions(permissions));
}
}
}
private class SessionStatusCallback implements Session.StatusCallback {
@Override
public void call(Session session, SessionState state,
Exception exception) {
// Check if Session is Opened or not
processSessionStatus(session, state, exception);
}
}
public void processSessionStatus(Session session, SessionState state,
Exception exception) {
if (session != null && session.isOpened()) {
if (session.getPermissions().contains("email")) {
// Show Progress Dialog
dialog = new ProgressDialog(FbLoginActivity.this);
dialog.setMessage("Logging in..");
dialog.show();
Request.executeMeRequestAsync(session,
new Request.GraphUserCallback() {
@Override
public void onCompleted(GraphUser user,
Response response) {
if (dialog != null && dialog.isShowing()) {
dialog.dismiss();
}
if (user != null) {
Map<String, Object> responseMap = new HashMap<String, Object>();
GraphObject graphObject = response
.getGraphObject();
responseMap = graphObject.asMap();
Log.i("FbLogin", "Response Map KeySet - "
+ responseMap.keySet());
// TODO : Get Email
// responseMap.get("email");
String fb_id = user.getId();
String email = null;
String name = (String) responseMap
.get("name");
if (responseMap.get("email") != null) {
email = responseMap.get("email")
.toString();
Intent i = new Intent(FbLoginActivity.this, FbLogin2Activity.class);
i.putExtra("Email", email);
startActivity(i);
} else {
// Clear all session info & ask user to
// login again
Session session = Session
.getActiveSession();
if (session != null) {
session.closeAndClearTokenInformation();
}
}
}
}
});
} else {
session.requestNewReadPermissions(new Session.NewPermissionsRequest(
FbLoginActivity.this, permissions));
}
}
}
/********** Activity Methods **********/
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
Log.d("FbLogin", "Result Code is - " + resultCode + "");
Session.getActiveSession().onActivityResult(FbLoginActivity.this,
requestCode, resultCode, data);
}
@Override
protected void onSaveInstanceState(Bundle outState) {
// TODO Save current session
super.onSaveInstanceState(outState);
Session session = Session.getActiveSession();
Session.saveSession(session, outState);
}
@Override
protected void onStart() {
// TODO Add status callback
super.onStart();
Session.getActiveSession().addCallback(statusCallback);
}
@Override
protected void onStop() {
// TODO Remove callback
super.onStop();
Session.getActiveSession().removeCallback(statusCallback);
}
}
然后我收到以下错误
09-12 15:28:07.728: E/AndroidRuntime(30897): FATAL EXCEPTION: main
09-12 15:28:07.728: E/AndroidRuntime(30897): java.lang.UnsupportedOperationException: Session: an attempt was made to open an already opened session.
09-12 15:28:07.728: E/AndroidRuntime(30897): at com.facebook.Session.open(Session.java:947)
09-12 15:28:07.728: E/AndroidRuntime(30897): at com.facebook.Session.openForRead(Session.java:385)
09-12 15:28:07.728: E/AndroidRuntime(30897): at com.test.app.FbLoginActivity$1.onClick(FbLoginActivity.java:48)
09-12 15:28:07.728: E/AndroidRuntime(30897): at android.view.View.performClick(View.java:4278)
09-12 15:28:07.728: E/AndroidRuntime(30897): at android.view.View$PerformClick.run(View.java:17430)
09-12 15:28:07.728: E/AndroidRuntime(30897): at android.os.Handler.handleCallback(Handler.java:725)
09-12 15:28:07.728: E/AndroidRuntime(30897): at android.os.Handler.dispatchMessage(Handler.java:92)
09-12 15:28:07.728: E/AndroidRuntime(30897): at android.os.Looper.loop(Looper.java:213)
09-12 15:28:07.728: E/AndroidRuntime(30897): at android.app.ActivityThread.main(ActivityThread.java:5092)
09-12 15:28:07.728: E/AndroidRuntime(30897): at java.lang.reflect.Method.invokeNative(Native Method)
09-12 15:28:07.728: E/AndroidRuntime(30897): at java.lang.reflect.Method.invoke(Method.java:511)
09-12 15:28:07.728: E/AndroidRuntime(30897): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:797)
09-12 15:28:07.728: E/AndroidRuntime(30897): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:564)
09-12 15:28:07.728: E/AndroidRuntime(30897): at dalvik.system.NativeStart.main(Native Method)
但是有时它在某些设备上运行良好。