AndroidAnnotations——SharedPreferencesHelpers

SharedPreferencesHelpers

Since AndroidAnnotations 2.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  @SharedPref to define the SharedPreferences :
首先,你应该生成一个加了   @SharedPref 注解的接口来定义SharedPreferences:

@SharedPref
public 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  @Pref annotation.
基于上述说明,AA会创建一个 SharedPreferences 辅助,它的文件名字和父类相同,只是附加一个下划线。你可以使用   @Pref   注解在任意优化的类中获取生成的辅助的实例。

Important: The type of the field  MUST be the generated class instead of the source class. It's the only exception in AA.
重要提示:字段类型 必须是生成类。这是AA中唯一的特例。
@EActivity
public 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 AndroidAnnotations 3.0


It's now possible to inject a default value from Android resources with  @DefaultRes:
目前可以使用   @DefaultRes 注解从Android资源注入默认值:

@SharedPref
public interface MyPrefs {
    @DefaultRes(R.string.defaultPrefName)
    String resourceName();

    @DefaultRes
    String defaultPrefAge();
}

Scope


Observe that you can name the shared preference by setting  value to one of the following:
需要注意的是你可以设置   value 为以下一项来命名 shared preference
  • ACTIVITY, for a shared preference named MyActivity_MyPrefs;
  • ACTIVITY, 则shared preference 命名MyActivity_MyPrefs;
  • ACTIVITY_DEFAULT, for a shared preference named MyActivity (also available throughactivity.getPreferences());
  • ACTIVITY_DEFAULT, 则shared preference 命名为MyActivity (也可以通过activity.getPreferences()获取);
  • APPLICATION_DEFAULT, for the default SharedPreference or UNIQUE, for a shared preference named MyPrefs.
  • 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 {
...

本文档的简单示例下载

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 4
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值