簡體   English   中英

Facebook身份驗證第二次未調用onActivityResult

[英]Facebook authentication not call onActivityResult from second time

我正在嘗試通過使用以下方式進行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函數調用。 有時我的服務重新啟動,會話對象變為null,所以我需要再次進行身份驗證,再次執行時,onActivityResult函數永遠不會調用,並且我的應用程序標題出現黑屏(活動屏幕),我必須按后退按鈕使其消失。 除此之外,一切工作正常,我得到了我的會議以及我在facebook上進行的操作均正常。

有沒有辦法強制黑屏關閉? 有沒有更好的方法可以進行身份​​驗證? 我知道我做的事情並不那么瑣碎,但我必須這樣做,我必須根據服務進行Facebook操作,並根據單獨的活動進行身份驗證。

如果會話已被緩存(即用戶先前已授權您的應用,並且您未執行session.closeAndClearTokenInformation),則會話將立即轉換為OPENED狀態,而無需啟動其他活動(因此不會調用onActivityResult )。

在您的FacebookAuthentication活動中,您應該在Session.openActiveSession調用之后執行以下操作:

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM