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();
}
}