【问题标题】:Where is the best place to store globals (auth token) in Android在 Android 中存储全局变量(身份验证令牌)的最佳位置在哪里
【发布时间】:2013-09-11 23:02:48
【问题描述】:

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

【问题讨论】:

  • Dropbox 和 Box sdk 示例应用程序使用 SharedPreferences 来存储身份验证令牌。似乎是要走的路。

标签: android authentication access-token


【解决方案1】:

SharedPreferences 是要走的路。 在此处查看文档:https://developer.android.com/reference/android/content/SharedPreferences.html

一些示例代码如下所示。

保存令牌:

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

获取令牌:

SharedPreferences settings = PreferenceManager
                            .getDefaultSharedPreferences(context);
String auth_token_string = settings.getString(some_key, ""/*default value*/);

【讨论】:

  • 您可能希望在保存到 sharedprefs 之前对其进行加密
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2012-08-31
  • 1970-01-01
  • 2011-02-09
  • 2018-01-05
  • 2014-04-05
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多