我在 Android 2.3.3 (HTC Wildifre S) 上登录 Facebook 时遇到问题。当我点击登录 Facebook 按钮时,它会将我重定向到 Facebook 应用程序 - 我在那里输入了我的登录名和密码。然后我被重定向到网络浏览器,我必须在网页上再次登录,然后网络浏览器将我重定向到fbconnect://success?access_token=xxxxxxxxxxxxx?expires_at=yyyyy 之类的 URL 。Web 浏览器无法处理此 URL,因此我收到 page not found 错误,并且我没有再次重定向到我的应用程序。
我有 Facebook 应用程序,我在配置中添加了包和类名,我已经在我的 Android 应用程序中正确配置了 appId,我生成了密钥哈希并放入我的 Facebook 应用程序配置中。
奇怪的是它在 Android 4.1 (Samsung Galaxy S Plus) 上运行。
这是我的活动代码:
package com.example.apptest;
import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;
import com.facebook.LoggingBehavior;
import com.facebook.Session;
import com.facebook.SessionState;
import com.facebook.Settings;
import com.facebook.android.Facebook;
public class FBLogin extends Activity {
public static final int GAME_THREAD_DELAY = 4000;
//private static final String URL_PREFIX_FRIENDS = "https://graph.facebook.com/me/friends?access_token=";
Facebook fb;
SharedPreferences sp;
private Button button_fblogin;
private Session.StatusCallback statusCallback = new SessionStatusCallback();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_fblogin);
Toast.makeText(FBLogin.this, "onCreate", Toast.LENGTH_SHORT).show();
button_fblogin = (Button) findViewById(R.id.fblogin_btn);
Settings.addLoggingBehavior(LoggingBehavior.INCLUDE_ACCESS_TOKENS);
Session session = Session.getActiveSession();
if (session == null){
Toast.makeText(FBLogin.this, "session == null", Toast.LENGTH_SHORT).show();
if (savedInstanceState != null){
session = Session.restoreSession(this, null, statusCallback, savedInstanceState);
}
if (session == null){
Toast.makeText(FBLogin.this, "session= new Session()", Toast.LENGTH_SHORT).show();
session = new Session(this);
}
Session.setActiveSession(session);
if (session.getState().equals(SessionState.CREATED_TOKEN_LOADED)){
session.openForRead(new Session.OpenRequest(this).setCallback(statusCallback));
}
}
updateView();
}
@Override
public void onStart(){
super.onStart();
Session.getActiveSession().addCallback(statusCallback);
}
@Override
public void onStop(){
super.onStop();
Session.getActiveSession().removeCallback(statusCallback);
}
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
Session.getActiveSession().onActivityResult(this, requestCode, resultCode, data);
}
@Override
protected void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
}
private class SessionStatusCallback implements Session.StatusCallback {
@Override
public void call(Session session, SessionState state, Exception exception) {
updateView();
}
}
private void updateView() {
Toast.makeText(FBLogin.this, "updateView", Toast.LENGTH_SHORT).show();
Session session = Session.getActiveSession();
if (session.isOpened()) {
button_fblogin.setText(R.string.button_fblogout);
button_fblogin.setOnClickListener(new OnClickListener() {
public void onClick(View view) { onClickLogout(); }
});
} else {
button_fblogin.setText(R.string.fblogin_button_title);
button_fblogin.setOnClickListener(new OnClickListener() {
public void onClick(View view) { onClickLogin(); }
});
}
}
private void onClickLogin(){
Session session = Session.getActiveSession();
if (!session.isOpened() && !session.isClosed()){
session.openForRead(new Session.OpenRequest(this).setCallback(statusCallback));
Toast.makeText(FBLogin.this, "onClickLogin", Toast.LENGTH_SHORT).show();
}else{
Session.openActiveSession(this, true, statusCallback);
}
}
private void onClickLogout(){
Session session = Session.getActiveSession();
if (!session.isClosed()){
session.closeAndClearTokenInformation();
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.fblogin, menu);
return true;
}
}