Android 为什么AccountManager.addaccount会显式返回false?

Android 为什么AccountManager.addaccount会显式返回false?,android,android-syncadapter,android-account,android-authenticator,Android,Android Syncadapter,Android Account,Android Authenticator,谷歌的Android文档()说: 返回 如果成功添加帐户,则为True;如果成功添加帐户,则为false 已存在,帐户为空,或发生其他错误 我越来越假了。具体来说,还有哪些错误会导致这种情况 false if the account already exists 如果没有提供任何信息,这可能是您获得错误信息的原因AccountManager服务是管理帐户的实际系统服务,而AccountManager只是一个隐藏所有绑定服务相关内容的代理 来自AccountManagerService的addA

谷歌的Android文档()说:

返回

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

我越来越假了。具体来说,还有哪些错误会导致这种情况

false if the account already exists

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

AccountManager服务
是管理帐户的实际系统服务,而
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;
}
private boolean addAccountInternal(用户帐户、帐户帐户、字符串密码、,
Bundle extras,boolean restricted,int callingUid){
如果(帐户==null){
返回false;
}
已同步(accounts.cacheLock){
final SQLiteDatabase db=accounts.openHelper.getWritableDatabase();
db.beginTransaction();
试一试{
long numMatches=DatabaseUtils.longForQuery(db,
“从”+表中选择计数(*)
+其中“+科目名称+”=?和“+科目类型+”=?”,
新字符串[]{account.name,account.type});
如果(数值匹配>0){
Log.w(标记“InsertAccountToDatabase:+帐户
+“,跳过,因为该帐户已存在”);
返回false;
}
ContentValues=新的ContentValues();
value.put(ACCOUNTS\u NAME,account.NAME);
value.put(ACCOUNTS\u TYPE,account.TYPE);
值。put(帐户\密码、密码);
value.put(ACCOUNTS\u LAST\u AUTHENTICATE\u TIME\u EPOCH\u MILLIS,System.currentTimeMillis());
long accountId=db.insert(表\u ACCOUNTS、ACCOUNTS\u NAME、值);
if(accountId<0){
Log.w(标记“InsertAccountToDatabase:+帐户
+“,跳过数据库插入失败”);
返回false;
}
如果(附加值!=null){
for(字符串键:extras.keySet()){
最终字符串值=extras.getString(键);
if(insertExtraLocked(db、accountId、key、value)<0){
Log.w(标记“InsertAccountToDatabase:+帐户
+,跳过,因为键“+键”的insertExtra失败;
返回false;
}
}
}
db.setTransactionSuccessful();
日志记录(db,DebugDbHelper.ACTION\u ACCOUNT\u ADD,TABLE\u ACCOUNTS,accountId,
账户,callingUid);
InsertAccountToCacheLocked(帐户、帐户);
}最后{
db.endTransaction();
}
sendAccountsChangedBroadcast(accounts.userId);
}
if(accounts.userId==UserHandle.USER\u所有者){
addAccountToLimitedUsers(帐户);
}
返回true;
}

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

请确保已连接到internet!就我而言,这就是问题所在

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
}

日志输出中有什么内容吗?这是从应用程序还是您编写的验证器调用的?您可以共享代码片段吗?