我的应用程序中有一个登录活动,用户输入电子邮件和密码并按下登录按钮,然后它会检查远程 mysql 数据库以查看用户是否输入了正确的详细信息,如果输入了则登录。我一直在努力让 facebook 登录与我当前的登录集成,我从这个脚本开始https://gist.github.com/vishalpawale/5560725并且目前拥有它,因此用户单击 facebook 登录按钮并被带到以下活动:
我登录中的按钮代码:
fblogin.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Intent i = new Intent(getApplicationContext(), FbLoginActivity.class);
startActivity(i);
}
});
FbLogin活动:
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 == null) {
Session.openActiveSession(FbLoginActivity.this, true,
statusCallback);
} else if (!session.isOpened()) {
session.openForRead(new Session.OpenRequest(
FbLoginActivity.this).setCallback(statusCallback)
.setPermissions(permissions));
}
}
});
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);
}
}
这很好用,但是它确实会带来一个新的活动来实际登录,并且您必须在 facebook 登录弹出窗口显示之前按下 FbLoginActivitys 视图上的按钮。我现在要做的是将 FbLoginActivity 集成到登录活动中,这样当按下 fblogin 按钮时,facebook 弹出窗口就会直接出现,而无需按下另一个按钮。