我试图让简单的登录活动按照 Facebook 开发人员页面上的描述工作:https ://developers.facebook.com/docs/getting-started/facebook-sdk-for-android/3.0/
到目前为止我的代码:
package com.xxxx.android;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
import com.actionbarsherlock.app.SherlockActivity;
import com.facebook.Request;
import com.facebook.Response;
import com.facebook.Session;
import com.facebook.SessionState;
import com.facebook.model.GraphUser;
public class MyTestsActivity extends SherlockActivity implements OnClickListener{
private Button mFbLogin;
private TextView mWelcome;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_my_tests);
setViews();
}
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
}
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
switch(v.getId()) {
case R.id.bFbLogin:
facebookLogin();
break;
}
}
private void facebookLogin() {
// TODO Auto-generated method stub
// start Facebook Login
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()) {
// 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) {
mWelcome.setText("Hello " + user.getName() + "!");
}
}
});
}
}
});
}
private void setViews() {
mFbLogin = (Button) findViewById(R.id.bFbLogin);
mFbLogin.setOnClickListener(this);
mWelcome = (TextView) findViewById(R.id.tvWelcome);
}
}
应用程序加载,我按下登录按钮,它打开一个对话框提示我允许访问应用程序,在我按下是后,活动恢复到原来的样子。textview 不会使用 facebook 提供的 onCompleted 方法中应该发生的用户信息进行更新。有任何想法吗?