我使用3.6来实现facebook登录。从官方文件来看,这是非常抽象的,不容易理解。所以我在互联网上找到了一些教程。但是,当我使用该教程中的代码时,它表示该方法已被弃用。虽然它有效但我担心官方会在以后停止这项功能。那么,有没有办法实现登录/发布到最新的sdk /获取个人信息?感谢
登录代码:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
lblEmail = (TextView) findViewById(R.id.t);
LoginButton authButton = (LoginButton) findViewById(R.id.authButton);
authButton.setOnErrorListener(new OnErrorListener() {
@Override
public void onError(FacebookException error) {
Log.i(TAG, "Error " + error.getMessage());
}
});
// set permission list, Don't foeget to add email
authButton.setReadPermissions(Arrays.asList("basic_info", "email"));
// session state call back event
authButton.setSessionStatusCallback(new Session.StatusCallback() {
@Override
public void call(Session session, SessionState state,
Exception exception) {
if (session.isOpened()) {
Log.i(TAG, "Access Token" + session.getAccessToken());
Request.executeMeRequestAsync(session,
new Request.GraphUserCallback() {
@Override
public void onCompleted(GraphUser user,
Response response) {
if (user != null) {
Log.i(TAG, "User ID " + user.getId());
Log.i(TAG,
"Email "
+ user.asMap().get(
"email"));
lblEmail.setText(user.asMap()
.get("email").toString());
}
}
});
}
}
});
}
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
Session.getActiveSession().onActivityResult(this, requestCode,
resultCode, data);
}
在代码中,Request.executeMeRequestAsync已弃用
答案 0 :(得分:1)
Button button =(Button) findViewById(R.id.button_share);
button.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
if (fb_session != null && fb_session.isOpened()) {
makeMeRequest(fb_session);
Log.i("Facebook Login State == >", "Facebook Login State");
} else {
if (fb_session == null) {
fb_session = new Session(mCurrContext);
}
Session.setActiveSession(fb_session);
ConnectToFacebook();
Log.i("Facebook not Login State == >", "Facebook Not login State");
}
}
});
}