2015-04-30 59 views
0

我正在使用sdk 4.0.1進行Facebook集成,它工作正常我從中獲得了數據,但是如果您的手機上已經安裝了Facebook應用程序,那麼我的登錄按鈕的臉書不起作用。它從不顯示任何登錄對話框。 如果我卸載我的Facebook應用程序,它會顯示並正常工作。爲什麼出現這些問題?任何人有任何解決方案?爲什麼如果facebook應用程序安裝在手機上,Facebook登錄對話框不會打開

+0

當你按下按鈕時,它發生了什麼?例如。 fb應用程序顯示雖然一會兒? – RediOne1

+0

@ RediOne1那麼,我該怎麼做呢? –

回答

0

恰到好處此行

LoginButton loginBtn = (LoginButton) findViewById(R.id.fb_login_button); 
    loginBtn.setLoginBehavior(SessionLoginBehavior.SUPPRESS_SSO); 

和文檔閱讀Facebook API ..

SUPPRESS_SSO 指定SSO不應該嘗試,並且只使用對話框權威性。 公共靜態最終LoginBehavior SUPPRESS_SSO

+0

請告訴我我如何聲明「SessionLoginBehavior」? –

+0

這是一個靜態的方法...只是寫這兩行,你會看到區別... 你在你的課堂上輸入了這段代碼..? – GvSharma

+0

是的!但它問我的SessionLoginBehavior,所以我如何聲明它? –

0

試試這個代碼

//調用按鈕點擊這個方法從Facebook(採取簡單的按鈕)

private void logInwithFacebook() 
     { 

      Session currentSession = Session.getActiveSession(); 
      if (currentSession == null || currentSession.getState().isClosed()) 
      { 
       Session session = new Session.Builder(LoginActivity.this).build(); 
       Session.setActiveSession(session); 
       currentSession = session; 
      } 

      if (currentSession.isOpened()) 
      { 
       // Do whatever u want. User has logged in 

      } 
      else 

      if (!currentSession.isOpened()) 
      { 
       // Ask for username and password 
       OpenRequest op = new Session.OpenRequest(LoginActivity.this); 

       op.setLoginBehavior(SessionLoginBehavior.SSO_WITH_FALLBACK); 
       op.setCallback(new StatusCallback() 
       { 

        @SuppressWarnings("deprecation") 
        @Override 
        public void call(Session session, SessionState state, Exception exception) 
        { 

         System.out.println("State= " + state); 
         if (session.isOpened()) 
         { 
          System.out.println("Token=" + session.getAccessToken()); 
          Request.newMeRequest(session, new GraphUserCallback() 
          { 
           @Override 
           public void onCompleted(GraphUser user, Response response) 
           { 
            System.out.println("USer :: " 
              + user.toString()); 

            if (user != null) 
            { 
             System.out.println("User=" + user); 
            } 
            if (response != null) 
            { 
             try 
             { 
              System.out.println("USer ID :: " 
                + user.getId()); 
              System.out.println("USer FIrstname :: " 
                + user.getFirstName()); 
              System.out.println("USer Latname :: " 
                + user.getLastName()); 
              System.out.println("USer Birthdate :: " 
                + user.getBirthday()); 
              System.out.println("USer Gender :: " 
                + user.getInnerJSONObject().getString("gender")); 
              System.out.println("USer Profile pic :: " 
                + "http://graph.facebook.com/" 
                + user.getId() 
                + "/picture" 
                + "?type=large"); 
              System.out.println("USer EmailAddress :: " 
                + user.asMap().get("email")); 
              System.out.println("USer BIO :: " 
                + user.getInnerJSONObject().getString("bio")); 






              // 
             } 
             catch (Exception e) 
             { 

             } 
            } 
            callFacebookLogout(); 

           } 

          }).executeAsync(); 

         } 
         if (exception != null) 
         { 
          System.out.println("Some thing bad happened!"); 
          exception.printStackTrace(); 
         } 
        } 
       }); 

       List<String> permissions = new ArrayList<String>(); 
       permissions.add("user_birthday"); 
       permissions.add("email"); 
       permissions.add("public_profile"); 
       permissions.add("user_about_me"); 
       permissions.add("friends_birthday"); 

       op.setPermissions(permissions); 

       Session session = new Session.Builder(LoginActivity.this).build(); 
       Session.setActiveSession(session); 
       session.openForPublish(op); 
      } 

     } 

//退出

public void callFacebookLogout() 
    { 
     Session session = Session.getActiveSession(); 
     if (session != null) 
     { 
      if (!session.isClosed()) 
      { 
       session.closeAndClearTokenInformation(); 
      } 
     } 
     else 
     { 
      session = new Session(LoginActivity.this); 
      Session.setActiveSession(session); 
      session.closeAndClearTokenInformation(); 
     } 
    } 

// onActivity結果

@Override 
    protected void onActivityResult(int requestCode, int resultCode, Intent data) 
    { 
     // TODO Auto-generated method stub 
     super.onActivityResult(requestCode, resultCode, data); 
     // Facebook session 
     Session.getActiveSession().onActivityResult(this, requestCode, resultCode, data); 
    } 
相關問題