我有一个 android 应用程序,我必须通过它登录到 facebook。我正在使用 facebook sdk 的会话对象登录。但是,如果设备中安装了实际的 facebook 应用程序,则登录不起作用。
下面是我的代码
public class FacebookSettings extends FragmentActivity{
/** Called when the activity is first created. */
private static final int LOGIN = 0;
private static final int LOGGED_IN = 1;
private static final int FRAGMENT_COUNT = LOGGED_IN +1;
private Button publishButton;
private Fragment[] fragments = new Fragment[FRAGMENT_COUNT];
private Session mSession;
private boolean isResumed = false;
@Override
public void onPause() {
// TODO Auto-generated method stub
super.onPause();
uiHelper.onPause();
isResumed=false;
}
@Override
public void onResume() {
// TODO Auto-generated method stub
super.onResume();
uiHelper.onResume();
isResumed=true;
}
protected static final String LOG_TAG = "FACEBOOK_TEST";
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.fb_settings);
publishButton=(Button) findViewById(R.id.publishButton);
android.support.v4.app.FragmentManager fm = getSupportFragmentManager();
fragments[LOGIN] = fm.findFragmentById(R.id.Login_Fragment);
fragments[LOGGED_IN] = fm.findFragmentById(R.id.Logged_in_Fragment);
android.support.v4.app.FragmentTransaction transaction = fm.beginTransaction();
for(int i = 0; i < fragments.length; i++) {
transaction.hide(fragments[i]);
}
transaction.commit();
uiHelper = new UiLifecycleHelper(this, callBack);
uiHelper.onCreate(savedInstanceState);
}
private void showFragment(int fragmentIndex, boolean addToBackStack) {
android.support.v4.app.FragmentManager fm = getSupportFragmentManager();
android.support.v4.app.FragmentTransaction transaction = fm.beginTransaction();
for (int i = 0; i < fragments.length; i++) {
if (i == fragmentIndex) {
transaction.show(fragments[i]);
} else {
transaction.hide(fragments[i]);
}
}
if (addToBackStack) {
transaction.addToBackStack(null);
}
transaction.commit();
}
private void onSessionStateChange(Session session, SessionState state, Exception exception) {
// Only make changes if the activity is visible
if (isResumed) {
Log.d("facebook","isResumed \n\n\n\n"+state.name());
android.support.v4.app.FragmentManager manager = getSupportFragmentManager();
// Get the number of entries in the back stack
int backStackSize = manager.getBackStackEntryCount();
// Clear the back stack
for (int i = 0; i < backStackSize; i++) {
manager.popBackStack();
}
if (state.isOpened()) {
Log.d("facebook","State isOpened in on session state changed");
// If the session state is open:
// Show the authenticated fragment
publishButton.setVisibility(View.VISIBLE);
showFragment(LOGGED_IN, false);
} else if (state.isClosed()) {
Log.d("facebook","State isClosed in on session state changed");
// If the session state is closed:
// Show the login fragment
publishButton.setVisibility(View.INVISIBLE);
showFragment(LOGIN, false);
}
}
}
@Override
protected void onResumeFragments() {
// TODO Auto-generated method stub
super.onResumeFragments();
Session session = Session.getActiveSession();
if (session != null && session.isOpened()) {
// if the session is already open,
// try to show the selection fragment
Log.d("facebook","State isOpened in resume fragments\n\n\n");
publishButton.setVisibility(View.VISIBLE);
showFragment(LOGGED_IN, false);
} else {
// otherwise present the splash screen
// and ask the user to login.
Log.d("facebook","State isClosed in resume fragments\n\n\n");
publishButton.setVisibility(View.INVISIBLE);
showFragment(LOGIN, false);
}
}
private UiLifecycleHelper uiHelper;
private Session.StatusCallback callBack=new StatusCallback() {
@Override
public void call(Session session, SessionState state, Exception exception) {
// TODO Auto-generated method stub
Log.d("facebook","in status call back \n\n\n\n");
Log.d("facebook","state\n\n\n\n"+session.isClosed());
Log.d("facebook","state\n\n\n\n"+session.isOpened());
Log.d("facebook","state\n\n\n\n"+state.isClosed());
Log.d("facebook","state\n\n\n\n"+state.isOpened());
onSessionStateChange(session,state,exception);
}
};
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
uiHelper.onActivityResult(requestCode, resultCode, data);
}
@Override
public void onDestroy() {
super.onDestroy();
uiHelper.onDestroy();
}
@Override
public void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
uiHelper.onSaveInstanceState(outState);
}
}
请帮忙谢谢