Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why might AccountManager.addAccountExplicitly return false?

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?

like image 255
cja Avatar asked Mar 12 '14 17:03

cja


3 Answers

false if the account already exists

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

like image 155
Sohil Avatar answered Oct 07 '22 19:10

Sohil


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.

like image 30
Vasiliy Avatar answered Oct 07 '22 18:10

Vasiliy


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
}
like image 31
harsh_v Avatar answered Oct 07 '22 19:10

harsh_v