2013-02-07 212 views
2

我有一個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); 
} 


} 

請幫助 感謝

+0

是你整合臉譜3.0? – Richa

+0

耶.. facebook-sdk 3.0 – Sushma

+0

你可以從這裏獲得完整的登錄教程:http://www.demoadda.com/demo/android/login-with-facebook-android_108 – Kishan

回答

0

我們面臨着同樣的問題的種類,但它是在IPhone應用程序。當我們在iPhone中開發一個facbook應用程序時,如果安裝了fb應用程序本身,則登錄不起作用。 我們解析它,把Iphone應用程序包標識符放入我在Facebook開發者頁面中創建的Facebook應用程序中。

我希望當你爲db開發者頁面中的android創建一個fb應用程序時缺少這種類似的feild。請檢查一下。

+0

謝謝..你能請告訴我在哪裏在開發者頁面添加app_id – Sushma

2

我認爲你正在尋找這樣的:

<meta-data 
     android:name="com.facebook.sdk.ApplicationId" 
     android:value="@string/applicationId" /> 

使用這些元數據由Facebook的SDK會話連接到您的應用程序。 您還可以設置它,而使用構建器

 new Session.Builder(this).setApplicationId(Constants.Facebook.APP_ID) 

參考的AndroidManifest.xml中HelloFacebookSample

+0

是啊,這是存在於我的清單中,如果tis不存在,登錄將不可能。我可以通過應用程序登錄,我的應用程序僅在實際的facebook應用程序安裝時纔有問題在通電話 – Sushma

0

我認爲這個問題是關鍵的哈希,你有Facebook頁面上輸入不同的是創建一個會話來自Facebook應用程序發送的密鑰哈希。解決的辦法之一是對的onCreate(添加以下代碼)方法:

@Override 
public void onCreate(Bundle savedInstanceState) { 
super.onCreate(savedInstanceState); 

try { 
    PackageInfo info = getPackageManager().getPackageInfo(
      "com.facebook.samples.loginhowto", 
      PackageManager.GET_SIGNATURES); 
    for (Signature signature : info.signatures) { 
     MessageDigest md = MessageDigest.getInstance("SHA"); 
     md.update(signature.toByteArray()); 
     Log.d("KeyHash:", Base64.encodeToString(md.digest(), Base64.DEFAULT)); 
     } 
} catch (NameNotFoundException e) { 

} catch (NoSuchAlgorithmException e) { 

} 
... 

請以你的包信息com.facebook.samples.loginhowto。查看KeyHash的日誌並將其添加到開發者頁面。 有關其他信息,請查看Facebook開發者頁面facebook。 但請記住,這是臨時解決方案,很可能只適用於帳戶,從中獲得了Facebook應用程序的關鍵散列。