问题
I'm working on an Android app that uses the AccountManager to store accounts & credentials. One problem I've been having is that even though I pass in a bunch of String values to the AccountManager's addAccountExplicitly data bundle.
I have checked to make sure that the bundle is not empty and that it contains the values before adding the account. And most of the time this is the case, but every once in a while I get null when I query these values.
The null return values are more common after I've just deleted the account and re-added it.
I'm doing my query inside the onAccountsUpdated method of an OnAccountsUpdateListener implementation, so the account should be added and good to go, right?
Thanks for any help
AuthenticatorActivity
// in the AuthenticatorActivity
Bundle userData = new Bundle();
userData.put (k1, v1);
userData.put (k2, v2);
userData.put (k3, v3);
userData.put (k4, v4);
userData.put (k1, v1);
Account a = new Account ("acc name", "com.account.type");
AccountManager am = AccountManager.get(this);
OnAccountsUpdateListener listener = new OnAccountsUpdateListener() {
@Override
public void onAccountsUpdated(Account[] accounts) {
Account mine = findAccount(accounts, account); // match account name
notifySignedIn(mine); // tell the world you're signed in
am.removeOnAccountsUpdatedListener(this);
}
};
am.addOnAccountsUpdatedListener(listener, handler, false);
am.addAccountExplicitly(a, "themostsecurepwintheworld", userData);
Some other thread
AccountManager am = AccountManager.get(mContext);
final string value2 = am.getUserData(mAccount, k2);
if (TextUtils.isEmpty(value2)) {
Log.d("WTF", "value is empty");
}
回答1:
I am aware of one bug in Honeycomb plus that can cause your issue. If you try to getUserData of the account before it is registered all subsequent getUserData calls will return null.
If you look at the code. AccountManager has an in-memory cache that is backed by a sqlite database. Calling getUserData populates the in-memory cache userdata, even if it is not registered. If it is not registered it interprets that as no userdata. Registering an account only populates the database and does not invalidate the in-memory store.
This may be causing your issue.
The workaround is to remove the account before calling addAccountExplicitly.
Ya this issue sucks and is a HUGE bug in AccountManger IMO and allows third parties to essence DOS your app.
回答2:
Are you using an HTC device with Android 4? I got many reports of this issue from users of my sync apps. All of them used an HTC device with SDK level 15.
Looks like an HTC bug to me.
Several users reported that issue vanished after a reboot.
Update: Meanwhile we found a proper workaround, see https://stackoverflow.com/a/29776224/1558654
回答3:
I had a similar issue, but my problem turned out to be attempting to store a long userData value instead of a String. Converting my long to a string for storage, and parsing it into a long again when pulling it out did the trick for me.
回答4:
One potential cause for this may be calling AccountManager.getUserData()
on a background thread. The documentation says "It is safe to call this method from the main thread." Our app was calling AccountManager.getUserData()
on a background thread, which usually worked fine, but apparently isn't guaranteed to work.
来源:https://stackoverflow.com/questions/10657658/accountmanager-getuserdata-returning-null-despite-it-being-set