项目Github地址 https://github.com/NashLegend/AnyPref
有时候在写代码的时候经常会有一些要持久保存某个对象的需求,这时候如果动用Sqlite又觉得太重,使用SharedPreferences保存的话确实是轻量级了,但是还要针对对象的每个字段都要保存,可能要好多行代码,读取出来又是好多行代码,我们为什么不直接自动保存与读取对象中的字段呢,如果有保存几个不同对象的需求的话,那就能省下大量的代码了。
AnyPref是一个SharedPreferences工具类,它可以直接保存某个对象到SharedPreferences中,使用方法:
在工程根目录build.gradle添加jitpack:
allprojects {
repositories {
maven { url "https://jitpack.io" }
}
}
在使用app/build.gradle中添加:
dependencies {
compile 'com.github.NashLegend:AnyPref:1.2.1'
}
AnyPref的基本原理是使用反射读取字段名,并将类名作为SharedPreferences的name,将字段名作为SharedPreferences中的key保存字段,同时也支持通过注解来自定义SharedPreferences的name和key,默认它会将所有的SharedPreferences支持的public字段保存(static和final修饰的除外),也可以通过注解来排除某些不需要的字段。如果要保存的对象中还包含了另一个复杂子对象,比如Family类中有一个Son的字段,这时候Son对象默认是不会保存的,如果想同时保存这个子对象,需要添加PrefSub注解。同理如果要保存的对象中还包含一个ArrayList,要想保存这个ArrayList,需要添加PrefArrayList注解。
如何保存与读取数据呢?
在应用的Application的onCreate()
中添加如下代码(主要是为了省却后面要传入Context参数的麻烦)
AnyPref.init(this);
假设有一个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");
就是这么简单~
同时还有一些简化操作SharedPreferences读写任意数据的方法:
AnyPref.getPrefs("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", ""));
项目Github地址 https://github.com/NashLegend/AnyPref
亿速云「云服务器」,即开即用、新一代英特尔至强铂金CPU、三副本存储NVMe SSD云盘,价格低至29元/月。点击查看>>
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。