I'd like to use the android AccountManager
to sync my webservice and application (standard sync of contacts and calander) however, AccountManager
only appears to store a username and password. My web service takes three credentials: a username, a password and an account. What is the best practice for storing the third piece of information?
Asked
Active
Viewed 1.2k times
16
-
sorry for a silly one but can you not merge up UserName and Account and keep it a s logical 'UserName'? – Aug 15 '11 at 09:11
2 Answers
33
As pablisco explained, you can use AccountManager's ability to store arbitrary user data through addAccountExplicitly()'s userData Bundle parameter:
final Bundle extraData = new Bundle();
extraData.putString("someKey", "stringData");
boolean accountCreated = am.addAccountExplicitly(account, password, extraData);
Later on, for example in your Authenticator's getAuthToken() method, you can retrieve the data related to the account you are working with:
String myData = am.getUserData(account, "someKey");
Unfortunately as of this writing you can only retrieve Strings, so your data should be stored as a String when you first build the Bundle. Hope this helps someone.

Carlos N
- 1,616
- 14
- 15
-
I'm looking at using the account manager for my app, and I'd need to store things like emails and userIds. Is this safe to do using the above method? Or could any app read the id and email? – TMH Jun 03 '14 at 14:14
-
@TomHart - the documentation on getUserData() (the way other apps would read Account data) states: "This method requires the caller to hold the permission AUTHENTICATE_ACCOUNTS and to have the same UID as the account's authenticator.". That means that if setup properly, you only your app will have access to that data. However, the AccountManager is NOT a safe area to save any sensitive data. Physical access to the device allows anyone to read that data. See: http://developer.android.com/reference/android/accounts/AccountManager.html#getUserData(android.accounts.Account, java.lang.String) – Carlos N Jun 04 '14 at 15:27
-
1I did some testing an I saw that I could read other accounts data, I just needed to the key. Do you have any recommendation on how to safely store user ids and emails? All I can think is use a really obscure key for setUserData, like `.setUserData("Some^^!_Really_StrangeEMa1L_Key!", userEmail);` but if someone decompiles the app they could possible get the string I used. – TMH Jun 04 '14 at 15:37
-
@J.CarlosNavea is there any way to add an arraylist as userdata to accountmanager and can be retrieved?? – KJEjava48 Sep 11 '17 at 11:55
3
From Android's documentation it's supposed to be done with either the userData Bundle when the Account is added:
AccountManager manager = AccountManager.get(context);
manager.addAccountExplicitly(account, null, userData);
or adding explicitly the values:
manager.setUserData(account, KEY, value);
But I'm having trouble with this:
-
Thanks for the reply. I ended up abandoning the account manager. I had so much trouble with it in the end. Having said that it looks like my project is growing and I may well return to it. I'll check your answer out soon – Jon Wells Dec 04 '11 at 17:22