19

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

退货

如果帐户添加成功,则为 true,如果帐户已存在,则为 false,该帐户为 null,或者发生其他错误

我越来越假了。具体来说,还有哪些其他错误可能导致这种情况?

4

3 回答 3

7
false if the account already exists

如果没有提供任何信息,这可能是您弄错的原因

于 2016-08-11T11:57:59.863 回答
1

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

下面的addAccountInternal方法 from的源代码AccountManagerService几乎是不言自明的,除了如果你通过nullfor accountthenIllegalArgumentException将被抛出而不是执行这个方法:

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;
}

底线:如果所需的帐户已经存在,或者某些 SQLite 数据库错误阻止了帐户相关信息在数据库中的存储,addAccountExplicitly则将返回。false

于 2016-08-11T13:47:42.047 回答
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
}
于 2017-01-04T10:32:54.177 回答