Why might AccountManager.addAccountExplicitly return false?
Asked Answered
A

3

19

Google's Android docs (http://developer.android.com/reference/android/accounts/AccountManager.html#addAccountExplicitly(android.accounts.Account, java.lang.String, android.os.Bundle)) say:

Returns

True if the account was successfully added, false if the account already exists, the account is null, or another error occurs

I am getting false. Specifically, what other errors could cause this?

Adamic answered 12/3, 2014 at 17:37 Comment(2)
Was there anything in the log output? Is this being called from the application or the authenticators that you have written?Doering
can you share code snippet?Prosperus
A
7
false if the account already exists

Without any information provided, this could be the reason you are getting false

Armagh answered 11/8, 2016 at 11:57 Comment(0)
K
1

AccountManagerService is the actual system service that manages accounts, whereas AccountManager is just a proxy that hides all bound-services-related stuff under the hood.

The below source code of addAccountInternal method from AccountManagerService is pretty much self-explanatory, except that if you pass null for account then IllegalArgumentException will be thrown instead of execution of this method:

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

Bottom line: addAccountExplicitly will return false if either the required account already exists, or some SQLite database error prevented storage of account related information in database.

Kyphosis answered 11/8, 2016 at 13:47 Comment(0)
C
1

Make sure you are connected to internet! In my case this was the problem!

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
}
Cynarra answered 4/1, 2017 at 10:32 Comment(1)
That method does not use Internet, it just adds a new account locally.Dorcus

© 2022 - 2024 — McMap. All rights reserved.