hkercn / AnyPref

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

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

一个SharedPreferences工具类

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

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

在使用AnyPref的模块中添加:

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

在应用的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会被保存,但是ArrayList不能是基本类型的
    }

保存数据:

    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,在proguard配置文件中添加
-keep class net.nashlegend.demo.Sample{ *; }

或者

-keepclasseswithmembernames class net.nashlegend.demo.Sample {
   public <fields>;
}

net.nashlegend.demo.Sample改成对应的类

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实用工具类,可直接保存与读取实例对象,SharedPreferences中的ORM


Languages

Language:Java 100.0%