2012-04-27 80 views
5

我在處理身份驗證時對Android體系結構有一些懷疑。瞭解Android身份驗證的意圖

假設我撥打AccountManager.getAuthToken我需要進行身份驗證的特定帳戶。假設驗證因密碼錯誤而失敗。 AbstractAccountAuthenticator合同要求認證者返回BundleActivity通過KEY_INTENT處理用戶名/密碼輸入。

我的問題是:誰應該顯示UI? Android會自動檢測到KEY_INTENT是否存在並運行UI,或者我的代碼是否必須startActivity,其意圖體現在AccountManager的回覆中? AccountManager.addAccount同樣適用於通過Future接口捆綁結果。

我在哪裏可以找到關於這些主題的一些教程?

謝謝

回答

0

系統不會自動顯示活動時KEY_INTENT存在。 開始該活動由您決定。

下面是一些示例代碼:

private AccountManagerCallback<Bundle> mAccountManagerCallback = new AccountManagerCallback<Bundle>() { 

    public void run(AccountManagerFuture<Bundle> future) { 

     Bundle bundle; 
     try { 

      bundle = future.getResult(); 
      //if an intent was sent, start the required activity 
      if (bundle.containsKey(AccountManager.KEY_INTENT)) { 
       Intent intent = bundle.getParcelable(AccountManager.KEY_INTENT); 

       //clear the new task flag just in case, since a result is expected 
       int flags = intent.getFlags(); 
       flags &= ~Intent.FLAG_ACTIVITY_NEW_TASK; 
       intent.setFlags(flags); 

       startActivityForResult(intent, REQUEST_CODE_AUTH); 

     } else { 
      //otherwise, just get the credentials 
      if (bundle.containsKey(AccountManager.KEY_AUTHTOKEN)) { 
        String authToken = bundle.getString(AccountManager.KEY_AUTHTOKEN); 
        String userMail  = bundle.getString(AccountManager.KEY_ACCOUNT_NAME); 
        //use the credentials 
      } 
     } 
     } 
     catch(...) { 
     ... 
     //handle errors, maybe retry your getAuthToken() call 
     } 
    } 
} 

我希望這是你要找的人,但如果我沒有理解你的問題正確,請澄清。

乾杯!

+0

如果我不**使用回調怎麼辦?我問了這個問題,因爲我想知道當認證者返回一個Intent時UI和UI是否需要做什麼。系統會自動顯示它還是必須在呼叫者中實施代碼?我開始認爲只有系統可以運行該意圖,因爲它必須以與帳戶創建者相同的UID運行。我真的很困惑 – 2012-06-08 11:38:45

+0

[Add]我想你的第一句話是*答案:每個呼叫者必須實現所有的邏輯... – 2012-06-08 11:42:35