2013-11-22 18 views
0

我已經完成了相關的syncAdapter所有操作,但現在即時通訊卡在一個小問題Android如何執行SyncAdapter的任何UI操作?

身份驗證令牌

後2小時我的令牌只是過期,然後我需要顯示用戶的對話再次輸入自己的密碼以便他可以更新他的標記。

AccountManager.get(getContext()).getAuthToken(account, LoginActivity.ACCOUNT_TYPE, null, false, new AccountManagerCallback<Bundle>() { 

      @Override 
      public void run(AccountManagerFuture<Bundle> arg0) { 
       try { 
        arg0.getResult(); 
       } catch (OperationCanceledException e) { 
        // TODO Auto-generated catch block 
        e.printStackTrace(); 
       } catch (AuthenticatorException e) { 
        // TODO Auto-generated catch block 
        e.printStackTrace(); 
       } catch (IOException e) { 
        // TODO Auto-generated catch block 
        e.printStackTrace(); 
       } 

      } 
     }, null); 

我在onPerformSync上運行這個,但這不是打開一個活動。

回答

0

有兩個部分,以在AbstractThreadedSyncAdapter實現重寫onPerformSync方法做這

1),你需要使用方法blockingGetAuthToken()

  • 嘗試使用到

    • 獲取從AccountManager的AUTHCODE authcode來執行你的同步過程(即web服務調用或任何你使用它)
    • 如果上一步失敗,因爲authcode已經顯示(例如,您的Web serivce返回某種AUTHCODE過期消息),那麼你需要在你的AbstractAccountAuthenticator實現覆蓋getAuthToken()方法

      • 使用AccountManager到的使用方法invalidateAuthToken()

      2)無效通過AccountManager的AUTHCODE檢索用戶上次提供的密碼,並嘗試使用這些憑據從Web服務獲取新的授權碼。

    • 如果上一步失敗,則添加一個意向,以便將您的登錄活動打開到從getAuthToken()方法返回的包。 這將導致在登錄屏幕顯示

    @Override 
    public Bundle getAuthToken(AccountAuthenticatorResponse oResponse, Account oAccount, String strAuthTokenType, Bundle options) 
          throws NetworkErrorException { 
    
        // Validate the authentication type   
        if (!strAuthTokenType.equals("TODO: your auth token type URL here")) 
        { 
         final Bundle result = new Bundle(); 
         result.putString(AccountManager.KEY_ERROR_MESSAGE, "invalid authTokenType"); 
         return result; 
        } 
    
        // Try to get the password already stored in account manger, if there is one 
        final AccountManager oAccountManager = AccountManager.get(moContext); 
        final String strPassword = oAccountManager.getPassword(oAccount); 
        if (strPassword != null) 
        { 
         // TODO: Call the authentication web service method to get a fresh authcode 
         // Pass the strPassword and oAccount.name 
         Boolean blnVerified = //TODO: were the username + password authenticated? 
         String strNewAuthCode = //TODO: the new authcode returned by your authentication web service 
    
         // If it worked then return the result 
         if (blnVerified) 
         { 
          final Bundle result = new Bundle(); 
          result.putString(AccountManager.KEY_ACCOUNT_NAME, oAccount.name); 
          result.putString(AccountManager.KEY_ACCOUNT_TYPE, "TODO: your account type URI here"); 
          result.putString(AccountManager.KEY_AUTHTOKEN, strNewAuthCode); 
          return result; 
         } 
        } 
    
        // Password is missing or incorrect. Start the activity to ask user to provide the missing credentials. 
        // Open a UI form to get the user to input their username and password again 
        final Intent oIntent = new Intent(moContext, frmAccount_Auth.class); 
        oIntent.putExtra(AccountManager.KEY_ACCOUNT_AUTHENTICATOR_RESPONSE, oResponse); 
        final Bundle bundle = new Bundle(); 
        bundle.putParcelable(AccountManager.KEY_INTENT, oIntent); 
        return bundle; 
    } 
    
  • 相關問題