Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Store additional data in Android Account Manager

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?

like image 869
Jon Wells Avatar asked Aug 15 '11 09:08

Jon Wells


1 Answers

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.

like image 171
Carlos N Avatar answered Sep 21 '22 15:09

Carlos N