9

我对安卓很陌生。我想知道最好的地方是存储我从服务器上登录获得的身份验证令牌之类的东西。对于每个后续请求,我都必须发布此身份验证令牌。当然,我可以在某个地方保留一个全局类,但我只想知道一种好的/标准方法是存储跨activitiesintents. 谢谢!

4

1 回答 1

14

SharedPreferences is the way to go. See doc here: https://developer.android.com/reference/android/content/SharedPreferences.html

Some example code is like below.

To save the token:

SharedPreferences settings = PreferenceManager
                            .getDefaultSharedPreferences(context);
SharedPreferences.Editor editor = settings.edit();
editor.putString(some_key, your_auth_token_string);
editor.commit();

To get the token:

SharedPreferences settings = PreferenceManager
                            .getDefaultSharedPreferences(context);
String auth_token_string = settings.getString(some_key, ""/*default value*/);
于 2013-09-08T08:00:44.860 回答