SharedPreferencesHelpers

Since AndroidAnnotations2.1


SharedPreferences helpers allow you to use Android SharedPreferences, but in a typesafe manner, instead of using strings. SharedPreferences辅助可以让你使用Android SharedPreferences ,但是用了类型安全方式来代替strings。

Defining the preferences


First, you should create an interface annotated with @SharedPrefto define the SharedPreferences : 首先,你应该生成一个加了 @SharedPref 注解的接口来定义SharedPreferences:
@SharedPrefpublic interface MyPrefs {        // The field name will have default value "John"        @DefaultString("John")        String name();        // The field age will have default value 42        @DefaultInt(42)        int age();        // The field lastUpdated will have default value 0        long lastUpdated();}

Based on that specification, AndroidAnnotations builds a SharedPreferences Helper that has the same name plus an underscore. You can get an instance of the generated helper in any enhanced class with the @Prefannotation. 基于上述说明,AA会创建一个 SharedPreferences 辅助,它的文件名字和父类相同,只是附加一个下划线。你可以使用 @Pref 注解在任意优化的类中获取生成的辅助的实例。
Important:The type of the field MUSTbe the generated class instead of the source class. It's the only exception in AA. 重要提示:字段类型 必须是生成类。这是AA中唯一的特例。
@EActivitypublic class MyActivity extends Activity {        @Pref        MyPrefs_ myPrefs;                // ...}

You can then start using it: 然后你就可以开始使用了:
// Simple edit myPrefs.name().put("John");// Batch edit myPrefs.edit()  .name()  .put("John")  .age()  .put(42)  .apply();// Preference clearing: myPrefs.clear();// Check if a value exists: boolean nameExists = myPrefs.name().exists();// Reading a value long lastUpdated = myPrefs.lastUpdated().get();// Reading a value and providing a fallback default value long now = System.currentTimeMillis();long lastUpdated = myPrefs.lastUpdated().getOr(now);

Default resource value

Since AndroidAnnotations3.0


It's now possible to inject a default value from Android resources with @DefaultRes: 目前可以使用 @DefaultRes 注解从Android资源注入默认值:
@SharedPrefpublic interface MyPrefs {    @DefaultRes(R.string.defaultPrefName)    String resourceName();    @DefaultRes    String defaultPrefAge();}

Scope


Observe that you can name the shared preference by setting valueto one of the following: 需要注意的是你可以设置 value 为以下一项来命名 shared preference
  • ACTIVITY, for a shared preference namedMyActivity_MyPrefs;
  • ACTIVITY, 则shared preference 命名MyActivity_MyPrefs;
  • ACTIVITY_DEFAULT, for a shared preference namedMyActivity(also available throughactivity.getPreferences());
  • ACTIVITY_DEFAULT, 则shared preference 命名为MyActivity(也可以通过activity.getPreferences()获取);
  • APPLICATION_DEFAULT, for the defaultSharedPreferenceorUNIQUE, for a shared preference namedMyPrefs.
  • APPLICATION_DEFAULT, 默认为SharedPreference或者UNIQUE, 则shared preference 命名为MyPrefs.

Therefore, if a single shared preference is needed for the interface defined, in order to all activities of a given application to share the same preferences, the following should be used: 因此,假如需要一个单例shared preference,为了给定application的所有activity可以共享数据,可以使用以下方法:
@SharedPref(value=SharedPref.Scope.UNIQUE)public interface MyPrefs {...

本文档的简单示例下载

更多相关文章

  1. Android(安卓)生成缩略图
  2. Android栗子の图片验证码生成
  3. Android(安卓)ContentResolver 使用例子,读取联系人
  4. Android重写ViewPager修改滑动灵敏度
  5. android之转化inputstream为list
  6. Android--数据库操作辅助类:SQLiteOpenHelper
  7. android 端生成随机验证码 实现
  8. Android检测辅助功能是否开启
  9. 为什么说枚举更占内存,枚举原理是什么?

随机推荐

  1. Android(安卓)UI + Function
  2. Android(安卓)——单例封装SharedPrefere
  3. android与原生的JS交互
  4. 关于android如何获取屏幕分辨率的例子
  5. The android gradle plugin version 3.0.
  6. androidannotations gradle Android(安卓
  7. Android中在OnCreate时获得控件高度
  8. Android放大镜的实现
  9. Android(安卓)常用的ui(单选框 多选框 下
  10. android 通过代码创建页面组件