2014-01-11 111 views
2

林驗證使用的AccountManager的Android的AccountManager和登錄活動

我知道,我可以打電話getAuthTokenByFeatures如果沒有帳戶設置爲我特別ACCOUNTTYPE它啓動我的LoginActivity這正是我想要的,

在我的應用程序的用戶

我有一個BaseActivity,它在onCreate方法上做到這一點 然而,在啓動LoginActivity時,舊的活動仍然在堆棧上,因此通過按下後退按鈕用戶可以返回到前一個活動,這是一種行爲,不需要,我在BaseActibity.onCreate上的代碼如下

AccountManager manager = AccountManager.get(getBaseContext()); 

    manager.getAuthTokenByFeatures(
      AccountGeneral.ACCOUNT_TYPE, 
      AccountGeneral.AUTHTOKEN_TYPE_FULL_ACCESS, 
      null, 
      this, 
      null, 
      null, 
      new AccountManagerCallback<Bundle>() { 
       @Override 
       public void run(AccountManagerFuture<Bundle> future) { 
        Bundle bnd = null; 
        try { 
         bnd = future.getResult(); 
         final String authtoken = bnd.getString(AccountManager.KEY_AUTHTOKEN); 

         LOGV(TAG, "GetTokenForAccount Bundle is " + bnd); 

        } catch (Exception e) { 
         LOGE(TAG, "exception while getAuthTokenByFeatures", e); 
        } 
       } 
      } 
      , null); 

問題是:我該如何禁用後退行爲?如果是我programmaticly調用LoginActivity我只想對BaseActivity

+0

我假設您的基本活動是您所有其他活動的延伸。那麼後退按鈕的行爲有什麼問題?如果你不想登錄,你不應該讓用戶回到以前的活動嗎?而您的舊活動會提供一些視覺提示,告訴用戶您需要登錄才能查看特定內容。 – CChi

回答

8

調用finish()在您的AccountAuthenticatorActivity可以覆蓋後退按鈕行爲:

@Override 
public void onBackPressed() { 
    Intent result = new Intent(); 
    Bundle b = new Bundle(); 
    result.putExtras(b); 

    setAccountAuthenticatorResult(null); // null means the user cancelled the authorization processs 
    setResult(RESULT_OK, result); 
    finish(); 
} 

現在你可以到這個消除反應。在您的代碼中:

  @Override 
      public void run(AccountManagerFuture<Bundle> future) { 
       Bundle bnd = null; 
       try { 
        if (future.isCancelled()) { 
         // Do whatever you want. I understand that you want to close this activity, 
         // so supposing that mActivity is your activity: 
         mActivity.finish(); 
         return; 
        } 
        bnd = future.getResult(); 

        final String authtoken = bnd.getString(AccountManager.KEY_AUTHTOKEN); 

        LOGV(TAG, "GetTokenForAccount Bundle is " + bnd); 

       } catch (Exception e) { 
        LOGE(TAG, "exception while getAuthTokenByFeatures", e); 
       } 
      }