Skip to content

NashLegend/AnyPref

Repository files navigation

一个SharedPreferences工具类

在工程根目录build.gradle添加jitpack:

allprojects {
    repositories {
        maven { url "https://jitpack.io" }
    }
}

在使用AnyPref的模块中添加:

dependencies {
    compile 'com.github.NashLegend:AnyPref:1.2.6'
}

在应用的Application的onCreate()中添加如下代码(主要是为了省却后面要传入Context参数的麻烦)

    AnyPref.init(this);

1. 读写实例对象

假设有一个Sample类(所有注解都是非必须的)

    @PrefModel("prefName")//"prefName"表示保存SharedPreferences的name,可为任意String字符串,若不添加此注解则为类全名
    public class Sample {
    
        @PrefField("intFieldKey")//"intFieldKey"表示保存此值时的key,可为任意String字符串,若不添加此注解,则为此字段的字段名
        public int intField = 32;
        
        @PrefIgnore//添加此注解表示不保存这个字段
        public float floatField = 1.2345f;
        
        @PrefField(numDef = 110)//表示如果读取不到后使用的默认值
        public long longField = 95789465213L;
        
        public String stringField = "string";
        
        @PrefField(boolDef = true)
        public boolean boolField = false;
        
        @PrefField(value = "setValueWithSpecifiedKey", strDef = {"1", "2", "3", "4"})//默认值是[1,2,3,4]
        public Set<String> setValue = new LinkedHashSet<>(); 
        
        @PrefSub(nullable = false)//nullable表示取子对象的时候,子对象是否可以为null,默认是true
        public SubSample son1;//标注了@PrefSub的字段,虽然不是SharedPreferences支持的类型,但是仍会被保存
        
        @PrefArrayList(nullable = true, itemNullable = true)//nullable同上,itemNullable表示列表中的数据是否可以为null,默认为true
        public ArrayList<SubSample> sampleArrayList;//标注了@PrefArrayList的ArrayList会被保存,必须要有泛型信息,且不能是基本类型的,其限制与PrefModel相同
    }

保存数据:

    AnyPref.put(sample);
    //或者
    AnyPref.put(sample, "your prefName");第二个参数是自己定义的保存此类的sharedPreferences name,不是PrefModel定义的那个name

读取数据

    Sample sample = AnyPref.get(Sample.class);
    //或者
    Sample sample = AnyPref.get(Sample.class, "your prefName");
    //或者
    Sample sample = AnyPref.get(Sample.class, "your prefName", true);//第三个参数表示读取出来的对象是否可以为null,默认不为null

清除数据

    AnyPref.clear(Sample.class);
    //或者
    AnyPref.clear(Sample.class, "your prefName");

PS,对于实例对象的读写:

  1. 保存的对象必须支持无参构造函数,它是写代码时用到的Model对象或者一组Setting等,不是用来保存一些系统对象比如String,View的;
  2. 保存的对象的字段们中只保存SharedPreferences支持的以及标注了@PrefSub@PrefArrayList的字段;
  3. 标注了@PrefSub@PrefArrayList的类型要求同第一条
  4. 只会保存修饰符为public的字段,staticfinal的字段均不会保存;
  5. 不要有循环引用,标注了@PrefSub的对象中不要包含标注了@PrefSub的父对象的类,@PrefArrayList同理,否则会导致向下无限读取
如果使用了ProGuard,在要保护的类上添加注解@PrefModel,然后在proguard配置文件中添加
-keepattributes Signature
-keep class net.nashlegend.anypref.annotations.PrefModel
-keepclasseswithmembernames @net.nashlegend.anypref.annotations.PrefModel class * {
    public <fields>;
}

2. 读写任意数据

    AnyPref.getPrefs("sample")//或者new SharedPrefs("sample")
            .putLong("long", 920394857382L)
            .putInt("int", 63)
            .putString("string", "sample string");

    AnyPref.getPrefs(Sample.class)
            .beginTransaction()
            .putLong("long", 920394857382L)
            .putInt("int", 63)
            .putString("string", "sample string")
            .commit();

    SharedPrefs sharedPrefs = AnyPref.getPrefs("sample");
    System.out.println(sharedPrefs.getInt("int", 0));
    System.out.println(sharedPrefs.getLong("long", 0));
    System.out.println(sharedPrefs.getString("string", ""));

About

SharedPreferences实用工具类,可直接保存与读取实例对象

Resources

Stars

Watchers

Forks

Packages

No packages published

Languages