我最近升级了我的代码以使用 FB android SDK 3.0。我的应用程序有四个活动并且不使用片段。奇怪的是,我的代码在 android 2.3.4 上没有任何问题。但在 android 4.0.4 中,会话状态始终为“OPENING”。我正在使用 UiLifeCycleHelper,下面是启动器活动中的代码。
SharedPreferences mPrefs;
String access_token = mPrefs.getString("access_token", null);
Session currentSession = Session.getActiveSession();
if(currentSession == null || currentSession.getState().isClosed())
{
if(access_token!=null)
{
SharedPreferences.Editor editor = mPrefs.edit();
//One time upgrade from FB SDK 2.0 to 3.0
editor.putString("access_token", null);
editor.commit();
AccessToken new_access_token = AccessToken.createFromExistingAccessToken(access_token, null, null, null, null);
currentSession.open(new_access_token, myCallback);
Session.setActiveSession(currentSession);
}
else
{
Session.openActiveSession(this, true, myCallback);
}
}
else if (!currentSession.isOpened())
{
OpenRequest openRequest = new OpenRequest(this).setCallback(myCallback);
openRequest.setDefaultAudience(SessionDefaultAudience.FRIENDS);
openRequest.setPermissions(Arrays.asList(permissions));
openRequest.setLoginBehavior(SessionLoginBehavior.SSO_WITH_FALLBACK);
currentSession.openForRead(openRequest);
}
else if(currentSession.isOpened())
{
handleRequest(currentSession);
}
StatusCallback myCallback = new StatusCallback()
{
@Override
public void call(Session session, SessionState state, Exception exception)
{
handleRequest(session);
}
};
public void handleRequest(Session activeSession)
{
Request new_request = new Request(activeSession, "/me/friends", null, null, new Callback()
{
@Override
public void onCompleted(Response response)
{
//Process the response and make UI changes.
}
});
}
首先,我怀疑问题出在旧版本的 Facebook 应用程序(1.9)上。所以我升级到最新版本(3.2)。在 android 4.0.4 上问题仍然存在。
这是我在 onActivityResult 回调中的代码
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data)
{
super.onActivityResult(requestCode, resultCode, data);
Session.getActiveSession().onActivityResult(this, requestCode, resultCode, data);
uihelper.onActivityResult(requestCode, resultCode, data);
}
有什么问题吗?