返回
如果帐户已成功添加,则为True;如果帐户已存在、帐户为空或发生其他错误,则为false
我变得虚伪了。具体地说,还有什么其他错误可能导致这种情况?
发布于 2016-08-11 19:58:00
false if the account already exists
如果没有提供任何信息,这可能是您得到错误的原因
发布于 2016-08-11 21:47:42
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
。
发布于 2017-01-04 18:32:54
确保你是连接到互联网!在我的例子中,这就是问题所在!
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
}
https://stackoverflow.com/questions/22359545
复制相似问题