我正在开发一个连接到 Facebook 的 Android 应用程序。
这是我的代码:
public class FBLoginActivity extends Activity
{
/**
* Log's tag.
*/
private static final String TAG = "FBLoginActivity";
/**
* Widget to let user selects if he/she accepts conditions or not.
*/
private ToggleButton mTglBtnAcceptConditions;
/**
* Represents the button to Login into Facebook.
*/
private ImageButton mFBLoginButton;
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_fblogin);
getWidgetsReferences();
}
@Override
protected void onResume()
{
Log.v(TAG, "onResume");
super.onResume();
Session session = Session.getActiveSession();
if ((session != null) && (session.isOpened()))
{
// Disable Facebook login button because user is logged.
mFBLoginButton.setEnabled(false);
// Show a message to the user saying that is logged.
Toast.makeText(this,
getString(R.string.toast_message_your_are_logged_fb),
Toast.LENGTH_LONG).show();
}
}
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data)
{
Log.v(TAG, "onActivityResult");
super.onActivityResult(requestCode, resultCode, data);
Session.getActiveSession().onActivityResult(this, requestCode, resultCode, data);
}
/**
* This method handles when user does click on Facebook's Login button.
* @param view
*/
public void onFBLoginClick(View view)
{
openActiveSession(this, true, statusCallback);
}
/**
* Status CallBack for Facebook Open Session.
*/
Session.StatusCallback statusCallback = new Session.StatusCallback()
{
@Override
public void call(final Session session, SessionState state, Exception exception)
{
Log.v(TAG, "call");
if(session.isOpened())
{
Log.v(TAG, "isOpened");
// Show a dialog saying that we are storing user data into the
// phone.
final ProgressDialog savingDialog = new ProgressDialog(FBLoginActivity.this);
savingDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
savingDialog.setMessage(getString(R.string.dialog_message_saving_your_fb_data));
savingDialog.setCancelable(false);
savingDialog.show();
Request.executeMeRequestAsync(session, new Request.GraphUserCallback()
{
@Override
public void onCompleted(GraphUser user, Response response)
{
Log.v(TAG, "onCompleted");
if(user != null)
{
// Get Facebook's ID, name, gender, birthday and email.
String facebookId = user.getId();
String name = user.getName();
String gender = user.getProperty("gender").toString();
String birthday = user.getBirthday();
String email = user.getProperty("email").toString();
// Save facebook data on Shared Preferences
saveUserData(facebookId, name, gender, birthday, email);
// Save Facebook access token on Shared preferences.
saveAccessToken(session.getAccessToken());
// Close saving user's data dialog.
savingDialog.dismiss();
// Get and save Facebook Profile Picture on internal
// storage.
GetFacebookProfilePicture task =
new GetFacebookProfilePicture(FBLoginActivity.this);
task.execute(user.getId());
}
}
});
}
}
};
/**
* Open a Facebook's session to get user personal data. On the OpenRequest
* we set "user_birthday" and "email" permissions.
*
* @param activity
* @param allowLoginUI
* @param statusCallback
* @return Session opened with desired permissions.
*/
private static Session openActiveSession(Activity activity,
boolean allowLoginUI, Session.StatusCallback statusCallback)
{
OpenRequest openRequest = new OpenRequest(activity);
openRequest.setPermissions(Arrays.asList("user_birthday", "email"));
openRequest.setCallback(statusCallback);
Session session = new Session.Builder(activity).build();
if(SessionState.CREATED_TOKEN_LOADED.equals(session.getState()) || allowLoginUI)
{
Session.setActiveSession(session);
session.openForRead(openRequest);
return session;
}
return null;
}
}
此代码适用于 Android 模拟器和我的旧 HTC Desire。但是当我在三星 Galaxy S3 等最新设备上测试它时,它不起作用。
调试代码,我这里加了一个断点:if(session.isOpened())
对象session
是:
{Session state:OPENING, token:{AccessToken token:ACCESS_TOKEN_REMOVED permissions:[]}, appId:XXXXXXXXXXX}
发生了什么?我认为问题在于代码在最快的设备上不起作用。
你知道我在这里做的另一种方法吗?