2013-10-26 32 views
1

我試圖讓Facebook的身份驗證調用onActivityResult:Facebook驗證不使用從第二次

public class FacebookAuthentication extends Activity { 
    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 

     // start Facebook Login 
     Session.openActiveSession(this, true, new Session.StatusCallback() { 

      // callback when session changes state 
      @Override 
      public void call(Session session, SessionState state, 
        Exception exception) { 
       if (session.isOpened()) { 

        // make request to the /me API 
        Request.executeMeRequestAsync(session, 
          new Request.GraphUserCallback() { 

           // callback after Graph API response with user 
           // object 
           @Override 
           public void onCompleted(GraphUser user, 
             Response response) { 
            if (user != null) { 

            } 
           } 

          }); 

       } 
      } 
     }); 
    } 

    @Override 
    public void onActivityResult(int requestCode, int resultCode, Intent data) { 
     Session.getActiveSession().onActivityResult(this, requestCode, 
       resultCode, data); 
     finish(); 
    } 

} 

我把從我服務的這個活動,因爲我只能從我的服務做相關的Facebook的一些操作,我這樣做:

if (session == null) { 
    Intent activ = new Intent(uploadService, 
      FacebookAuthentication.class); 
    activ.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); 
    uploadService.startActivity(activ); 
    for (int i = 0; i < 20; i++) { 
     try { 
      if (Session.getActiveSession() != null) { 
       session = Session.getActiveSession(); 
       break; 
      } 
      Thread.sleep(3000); 
     } catch (InterruptedException e) { 
      Log.i(TAG, "Sleep failed"); 
     } 
    } 
    if (Session.getActiveSession() == null) { 

     return false; 
    } 
} 

所以這樣我得到我的會話對象,我可以使用它。第一次工作很好,我的onActivityResult函數調用。 有時我的服務會重新啓動,session對象會變爲null,所以我需要重新進行身份驗證,再次執行onActivityResult函數時永遠不會調用,並且我的應用程序標題會出現黑屏(活動屏幕),我必須按後退按鈕使其消失。除了這一切都很好,我得到了我的會議以及我在Facebook上所做的操作。

有沒有辦法強制黑屏關閉?有更好的方法可以進行身份​​驗證嗎? 我知道我正在做的事情並不那麼平凡,但我必須這樣做,我必須從服務和獨立活動的身份驗證進行Facebook操作。

回答

0

如果會話已被緩存(即用戶先前授權了您的應用程序,並且您未執行session.closeAndClearTokenInformation),則會話將立即轉換到OPENED狀態,而不會開始另一個活動(並因此獲勝't call onActivityResult)。

在你FacebookAuthentication活動,你應該將Session.openActiveSession電話後做這樣的事情:

Session session = Session.getActiveSession(); 
if (session != null && session.isOpened()) { 
    // that means the session is opened from cache 
    finish(); 
} 
相關問題