2014-10-27 78 views
1

我試圖在我的遊戲中整合Facebook登錄,但自從現在我無法實現這一點。Facebook集成在Libgdx

我按照指令:

https://github.com/libgdx/libgdx/wiki/Interfacing-with-platform-specific-code

而且創建了接口,並實現了它的Android項目,但仍然沒有成功。當我打電話給我的登錄Facebook功能時,它顯示我一個循環加載屏幕,我的遊戲屏幕回來。

public class AndroidFacebookAdapter implements FacebookIntegration { 

    private String name; 
    private Activity androidActivity; 

    public AndroidFacebookAdapter(Activity andAct) { 
     super(); 
     this.name = ""; 
     this.androidActivity = andAct ; 
    } 



    @Override 
    public void loginFacebook() { 

     Gdx.app.log("AndroidFacebookAdapter"," Facebook login starts"); 

     //Session.openActiveSession(this, allowLoginUI, callback) 
     androidActivity.runOnUiThread(new Runnable() { 

      @Override 
      public void run() { 
       Gdx.app.log("AndroidFacebookAdapter"," runOnUiThread runs"); 
       Session.openActiveSession(androidActivity, true, new Session.StatusCallback() { 

        // callback when session changes state 
        @Override 
        public void call(Session session, SessionState state, Exception exception) { 
         if (session.isOpened()) { 
         Gdx.app.log("AndroidFacebookAdapter"," session.isOpened returns true "); 
         // make request to the /me API 
         Request.newMeRequest(session, new Request.GraphUserCallback() { 

          // callback after Graph API response with user object 
          @Override 
          public void onCompleted(GraphUser user, Response response) { 
          if (user != null) { 
           Gdx.app.log("AndroidFacebookAdapter"," Facebook Login Succefull"); 
           name = user.getName(); 
          } 
          } 
         }).executeAsync(); 
         } 
        } 
        });    

      } 
     }); 


    } 

    @Override 
    public String getName() { 

     return name; 
    } 

} 

在我的Android項目中,我將這個接口發送給核心項目。

andAdap = new AndroidFacebookAdapter(this); 

    initialize(new MyGdxGame(andAdap), config); 

我在觸摸屏幕時調用它。

game.facebookIntegration.loginFacebook(); 

在logcat中,我看到:

10-27 13:06:47.988: I/AndroidFacebookAdapter(16774): Facebook login starts 
10-27 13:06:47.988: I/AndroidFacebookAdapter(16774): runOnUiThread runs 

後,我看到一個圓形的加載屏幕(沒有在屏幕上只顯示黑白),然後我的遊戲畫面再回來。在哪一步我犯了一個錯誤?

謝謝你的回答。

編輯1:在意識到我已經登錄我的Facebook帳戶後,我退出了。然後再試一次,它工作。它是開放的Facebook應用程序要求我的憑據,但即使我輸入正確的用戶名/密碼我的「onCompleted」功能不被稱爲。

編輯2:現在我的問題解決了,感謝教程在http://blog.supercookie.co.uk/post/94802569132/facebook-integration-w-libgdx 我只需要使用UiLifecycleHelper類和它的實現。現在我的界面實現了課堂。

public class AndroidFacebookAdapter implements FacebookIntegration { 

    private String name; 
    public AndroidLauncher androidActivity; 
    private final UiLifecycleHelper uiHelper; 

    public AndroidFacebookAdapter(Activity andAct) { 
     super(); 
     this.name = ""; 
     this.androidActivity = (AndroidLauncher) andAct ; 
     this.uiHelper = new UiLifecycleHelper(this.androidActivity, new Session.StatusCallback() { 
      @Override 
      public void call(Session session, SessionState sessionState, Exception e) { 

      } 
     }); 
    } 


    private void loginFacebookImp() { 

     Gdx.app.log("AndroidFacebookAdapter"," Facebook login starts"); 

     androidActivity.runOnUiThread(new Runnable() 
     { 

      @Override 
      public void run() 
      { 
       Gdx.app.log("AndroidFacebookAdapter"," runOnUiThread runs"); 
       Session.openActiveSession(androidActivity, true, new Session.StatusCallback() 
       { 

        // callback when session changes state 
        @Override 
        public void call(Session session, SessionState state, Exception exception) 
        { 
         Gdx.app.log("AndroidFacebookAdapter"," openActiveSession call start"); 
         if (session.isOpened()) 
         { 
          Gdx.app.log("AndroidFacebookAdapter"," session.isOpened returns true "); 
          // make request to the /me API 
          Request.newMeRequest(session, new Request.GraphUserCallback() 
          { 

            // callback after Graph API response with user object 
            @Override 
            public void onCompleted(GraphUser user, Response response) { 
             Gdx.app.log("AndroidFacebookAdapter"," onCompleted start"); 
            if (user != null) { 
             Gdx.app.log("AndroidFacebookAdapter"," Facebook Login Succesfull"); 
             Gdx.app.log("AndroidFacebookAdapter"," Facebook name is " + user.getName()); 
             name = user.getName(); 
            } 
            else 
            { 
             Gdx.app.log("AndroidFacebookAdapter"," Facebook Login failed"); 
            } 
            } 
          }).executeAsync(); 
         } 
         else 
         { 
          Gdx.app.log("AndroidFacebookAdapter"," session.isOpened() is false"); 
         } 
        } 
        });    

      } 
     }); 




    } 

    private boolean isLoggedInImp() 
    { 
     return Session.getActiveSession() != null && Session.getActiveSession().isOpened(); 
    } 

    @Override 
    public String getName() {  
     return name; 
    } 



    @Override 
    public void loginFacebook() { 
     loginFacebookImp();  
    } 

    public void onCreate(Bundle savedInstanceState) { 
     uiHelper.onCreate(savedInstanceState); 
    } 

    public void onResume() { 
     uiHelper.onResume(); 
    } 

    public void onActivityResult(int requestCode, int resultCode, Intent data) { 
     uiHelper.onActivityResult(requestCode, resultCode, data); 
    } 

    public void onSaveInstanceState(Bundle outState) { 
     uiHelper.onSaveInstanceState(outState); 
    } 

    public void onPause() { 
     uiHelper.onPause(); 
    } 

    public void onStop() { 
     uiHelper.onStop(); 
    } 

    public void onDestroy() { 
     uiHelper.onDestroy(); 
    } 


    @Override 
    public boolean isLoggedIn() { 

     return isLoggedInImp(); 
    } 

} 
+0

現在我可以打開facebook應用程序的身份驗證,但我不能得到用戶信息。編輯。 – bkaramik 2014-10-28 20:44:58

回答