为什么AccountManager.addAccount明显返回false?

时间:2014-03-12 17:37:43

标签: android android-syncadapter android-account android-authenticator

Google的Android文档(http://developer.android.com/reference/android/accounts/AccountManager.html#addAccountExplicitly(android.accounts.Account, java.lang.String, android.os.Bundle))说:

  

返回

     

如果帐户成功添加,则为True,如果是帐户,则为false   已存在,帐户为空,或发生其他错误

我变得虚假。具体来说,还有哪些其他错误会导致这种情况?

3 个答案:

答案 0 :(得分:7)

delta

如果没有提供任何信息,这可能是您获得虚假的原因

答案 1 :(得分:1)

确保您已连接到互联网!就我而言,这就是问题!

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
}

答案 2 :(得分:0)

AccountManagerService是管理帐户的实际系统服务,而AccountManager只是隐藏所有绑定服务相关内容的代理。

来自addAccountInternal的{​​{1}}方法的以下源代码几乎是不言自明的,除非您通过AccountManagerService null然后account将抛出而不是执行此方法:

IllegalArgumentException

底线:如果所需帐户已存在,或者某些SQLite数据库错误阻止在数据库中存储帐户相关信息,则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