19

谷歌的Android文檔(http://developer.android.com/reference/android/accounts/AccountManager.html#addAccountExplicitly(android.accounts.Account, java.lang.String, android.os.Bundle))說:爲什麼AccountManager.addAccount會顯式返回false?

返回

真,如果成功添加帳戶,如果帳戶 已經存在虛假,賬號爲空,或者發生其他錯誤

我越來越假。具體來說,還有哪些其他錯誤可能導致這種情況?

+0

有沒有在日誌輸出什麼?這是從應用程序調用還是由您編寫的身份驗證器調用? –

+0

你能分享代碼片段嗎? – RBK

回答

6
false if the account already exists 

沒有提供任何信息,這可能是你得到虛假的理由

0

AccountManagerService是管理賬戶實際的系統服務,而AccountManager只是隱藏所有綁定的服務相關的東西代理在引擎蓋下。

AccountManagerServiceaddAccountInternal方法的下面的源代碼是相當多的不言自明的,不同之處在於如果傳遞nullaccount然後IllegalArgumentException將被拋出,而不是本方法的執行:

private boolean addAccountInternal(UserAccounts accounts, Account account, String password, 
     Bundle extras, boolean restricted, int callingUid) { 
    if (account == null) { 
     return false; 
    } 
    synchronized (accounts.cacheLock) { 
     final SQLiteDatabase db = accounts.openHelper.getWritableDatabase(); 
     db.beginTransaction(); 
     try { 
      long numMatches = DatabaseUtils.longForQuery(db, 
        "select count(*) from " + TABLE_ACCOUNTS 
          + " WHERE " + ACCOUNTS_NAME + "=? AND " + ACCOUNTS_TYPE+ "=?", 
        new String[]{account.name, account.type}); 
      if (numMatches > 0) { 
       Log.w(TAG, "insertAccountIntoDatabase: " + account 
         + ", skipping since the account already exists"); 
       return false; 
      } 
      ContentValues values = new ContentValues(); 
      values.put(ACCOUNTS_NAME, account.name); 
      values.put(ACCOUNTS_TYPE, account.type); 
      values.put(ACCOUNTS_PASSWORD, password); 
      values.put(ACCOUNTS_LAST_AUTHENTICATE_TIME_EPOCH_MILLIS, System.currentTimeMillis()); 
      long accountId = db.insert(TABLE_ACCOUNTS, ACCOUNTS_NAME, values); 
      if (accountId < 0) { 
       Log.w(TAG, "insertAccountIntoDatabase: " + account 
         + ", skipping the DB insert failed"); 
       return false; 
      } 
      if (extras != null) { 
       for (String key : extras.keySet()) { 
        final String value = extras.getString(key); 
        if (insertExtraLocked(db, accountId, key, value) < 0) { 
         Log.w(TAG, "insertAccountIntoDatabase: " + account 
           + ", skipping since insertExtra failed for key " + key); 
         return false; 
        } 
       } 
      } 
      db.setTransactionSuccessful(); 

      logRecord(db, DebugDbHelper.ACTION_ACCOUNT_ADD, TABLE_ACCOUNTS, accountId, 
        accounts, callingUid); 

      insertAccountIntoCacheLocked(accounts, account); 
     } finally { 
      db.endTransaction(); 
     } 
     sendAccountsChangedBroadcast(accounts.userId); 
    } 
    if (accounts.userId == UserHandle.USER_OWNER) { 
     addAccountToLimitedUsers(account); 
    } 
    return true; 
} 

底線: addAccountExplicitly將返回false如果任何所需的帳戶已存在,或者一些SQLite數據庫錯誤,無法在數據庫帳戶相關的信息存儲。

0

確保您連接到互聯網!在我的情況下,這是問題!

if (accountManager.addAccountExplicitly(_account, null, null)) { 
     System.out.println("_add account if"); 
    }else { 
     // This block is also executed in case device has no internet connection 
}