当我在 Android 应用程序中使用 Facebook 登录时,会在显示权限弹出窗口之前调用回调。在我的代码中,预期的行为应该是:
- 用户打开应用
- 用户点击“登录”按钮
- 弹出FB权限对话框
- 用户接受 FB 权限
- “嗨!” 显示吐司消息
但是,实际流程是:
- 用户打开应用
- 用户点击“登录”按钮
- “嗨!” 显示吐司消息
- 弹出FB权限对话框
我基本上使用了这个答案中的代码,并添加了一个按钮来在用户点击它时执行登录,而不是在创建活动时执行登录:
public class MainActivity extends Activity implements StatusCallback {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void signIn(View view) {
OpenRequest open = new OpenRequest(this);
open.setLoginBehavior(SessionLoginBehavior.SUPPRESS_SSO);
open.setPermissions(Arrays.asList(new String[] { "email", "user_hometown" }));
open.setCallback(this);
Session s = new Session(this);
s.openForRead(open);
}
@Override
public void call(Session session, SessionState state, Exception exception) {
CharSequence text = "Hi there!";
int duration = Toast.LENGTH_SHORT;
Toast toast = Toast.makeText(getApplicationContext(), text, duration);
toast.show();
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (Session.getActiveSession() != null)
Session.getActiveSession().onActivityResult(this, requestCode,
resultCode, data);
}
}
谢谢