我有一個Android應用程序,我必須登錄到Facebook。 我正在使用facebook sdk的會話對象進行登錄。 但是,如果設備安裝了實際的Facebook應用程序,則登錄不起作用。android 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);
}
}
請幫助 感謝
是你整合臉譜3.0? – Richa
耶.. facebook-sdk 3.0 – Sushma
你可以從這裏獲得完整的登錄教程:http://www.demoadda.com/demo/android/login-with-facebook-android_108 – Kishan