Store additional data in Android Account Manager

喜欢而已 提交于 2020-01-13 07:40:30

问题


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?


回答1:


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.




回答2:


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:

AccountManager IllegalArgumentException: key is null



来源:https://stackoverflow.com/questions/7063280/store-additional-data-in-android-account-manager

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!