使用 Facebook sdk,如果用户输入凭据,则在 facebook 登录屏幕中,然后应用程序显示 facebook 错误 toast 消息。我如何通过在此处放置 login() 代码成功登录来解决此问题。
protected void loginToFacebook() {
// TODO Auto-generated method stub
mPrefs = getPreferences(MODE_PRIVATE);
String access_token = mPrefs.getString("access_token", null);
long expires = mPrefs.getLong("access_expires", 0);
if (access_token != null) {
facebook.setAccessToken(access_token);
}
if (expires != 0) {
facebook.setAccessExpires(expires);
}
if (!facebook.isSessionValid()) {
facebook.authorize(this,
new String[] { "email", "publish_stream" },
new DialogListener() {
@Override
public void onCancel() {
// Function to handle cancel event
Toast.makeText(getApplicationContext(), "Login cancel", Toast.LENGTH_LONG).show();
}
@Override
public void onComplete(Bundle values) {
// Function to handle complete event
// Edit Preferences and update facebook acess_token
SharedPreferences.Editor editor = mPrefs.edit();
editor.putString("access_token",
facebook.getAccessToken());
editor.putLong("access_expires",
facebook.getAccessExpires());
editor.commit();
}
@Override
public void onError(DialogError error) {
// Function to handle error
Toast.makeText(getApplicationContext(), "error in login", Toast.LENGTH_LONG).show();
}
@Override
public void onFacebookError(FacebookError fberror) {
// Function to handle Facebook errors
Toast.makeText(getApplicationContext(), "Facebook error", Toast.LENGTH_LONG).show();
}
});
}