我尝试了第一个教程以使用我的 android 应用程序连接到 facebook,它仅在我的手机上没有官方 Android 应用程序时才有效,但是当我这样做时,会话未打开
这是我的代码:
public class LoginActivity extends Activity {
TextView tv_name;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login);
tv_name = (TextView) findViewById(R.id.tv_activity_login);
startFbLogin();
}
private void startFbLogin() {
// start Facebook LoginActivity
Session.openActiveSession(this, true, new Session.StatusCallback() {
// callback when session changes state
@Override
public void call(Session session, SessionState state, Exception exception) {
if (session.isOpened()) {
Log.i("FaceBook", "session is Opened");
// make request to the /me API
Request.executeMeRequestAsync(session, new Request.GraphUserCallback() {
// callback after Graph API response with user object
@Override
public void onCompleted(GraphUser user, Response response) {
if (user != null) {
Log.i("FaceBook", "user is not null");
tv_name.setText("Hello " + user.getName() + "!");
}else{
Log.i("FaceBook", "user is null");
logout();
}
}
});
}else{
Log.i("FaceBook", "is not Opened");
}
}
});
}
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
Session.getActiveSession().onActivityResult(this, requestCode, resultCode, data);
}
private void logout(){
// clear any user information
//mApp.clearUserPrefs();
// find the active session which can only be facebook in my app
Session session = Session.getActiveSession();
// run the closeAndClearTokenInformation which does the following
// DOCS : Closes the local in-memory Session object and clears any persistent
// cache related to the Session.
session.closeAndClearTokenInformation();
// return the user to the login screen
startActivity(new Intent(getApplicationContext(), LoginActivity.class));
// make sure the user can not access the page after he/she is logged out
// clear the activity stack
finish();
}
}