为什么AccountManager.addAccountExplicitly可能会返回false?

19

日志输出中有什么内容吗?这是从您编写的应用程序还是认证器中调用的? - Ray Hunter
你能分享一下代码片段吗? - RBK
3个回答

7
false if the account already exists

如果没有提供任何信息,那么这可能是你得到错误结果的原因。


1

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

AccountManagerService 中的 addAccountInternal 方法的源代码相当自解释,除非您为 account 传递 null,否则将抛出 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;
}

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

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
}

该方法不使用互联网,只是在本地添加一个新帐户。 - Nicola Revelant
该方法不使用互联网,只是在本地添加一个新账户。 - undefined

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接