AnyPref,可直接在SharedPreferences中保存對象實例的SharedPreferences工具類

項目Github地址 https://github.com/NashLegend/AnyPref

成都創(chuàng)新互聯(lián)公司專注于企業(yè)成都全網(wǎng)營銷、網(wǎng)站重做改版、郯城網(wǎng)站定制設(shè)計、自適應(yīng)品牌網(wǎng)站建設(shè)、H5頁面制作、成都做商城網(wǎng)站、集團(tuán)公司官網(wǎng)建設(shè)、外貿(mào)網(wǎng)站制作、高端網(wǎng)站制作、響應(yīng)式網(wǎng)頁設(shè)計等建站業(yè)務(wù),價格優(yōu)惠性價比高,為郯城等各大城市提供網(wǎng)站開發(fā)制作服務(wù)。

有時候在寫代碼的時候經(jīng)常會有一些要持久保存某個對象的需求,這時候如果動用Sqlite又覺得太重,使用SharedPreferences保存的話確實是輕量級了,但是還要針對對象的每個字段都要保存,可能要好多行代碼,讀取出來又是好多行代碼,我們?yōu)槭裁床恢苯幼詣颖4媾c讀取對象中的字段呢,如果有保存幾個不同對象的需求的話,那就能省下大量的代碼了。

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,默認(rèn)它會將所有的SharedPreferences支持的public字段保存(static和final修飾的除外),也可以通過注解來排除某些不需要的字段。如果要保存的對象中還包含了另一個復(fù)雜子對象,比如Family類中有一個Son的字段,這時候Son對象默認(rèn)是不會保存的,如果想同時保存這個子對象,需要添加PrefSub注解。同理如果要保存的對象中還包含一個ArrayList,要想保存這個ArrayList,需要添加PrefArrayList注解。

如何保存與讀取數(shù)據(jù)呢?

在應(yīng)用的Application的onCreate()中添加如下代碼(主要是為了省卻后面要傳入Context參數(shù)的麻煩)

    AnyPref.init(this);

假設(shè)有一個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)//表示如果讀取不到后使用的默認(rèn)值
        public long longField = 95789465213L;
        
        public String stringField = "string";
        
        @PrefField(boolDef = true)
        public boolean boolField = false;
        
        @PrefField(value = "setValueWithSpecifiedKey", strDef = {"1", "2", "3", "4"})//默認(rèn)值是[1,2,3,4]
        public Set<String> setValue = new LinkedHashSet<>(); 
        
        @PrefSub(nullable = false)//nullable表示取子對象的時候,子對象是否可以為null,默認(rèn)是true
        public SubSample son1;//標(biāo)注了@PrefSub的變量,雖然不是SharedPreferences支持的類型,但是仍會被保存
        
        @PrefArrayList(nullable = true, itemNullable = true)//nullable同上,itemNullable表示列表中的數(shù)據(jù)是否可以為null,默認(rèn)為true
        public ArrayList<SubSample> sampleArrayList;//標(biāo)注了@PrefArrayList的ArrayList會被保存,但是ArrayList不能是基本類型的
    }

保存數(shù)據(jù):

    AnyPref.put(sample);
    //或者
    AnyPref.put(sample, "your prefName");第二個參數(shù)是自己定義的保存此類的sharedPreferences name,不是PrefModel定義的那個name

讀取數(shù)據(jù)

    Sample sample = AnyPref.get(Sample.class);
    //或者
    Sample sample = AnyPref.get(Sample.class, "your prefName");
    //或者
    Sample sample = AnyPref.get(Sample.class, "your prefName", true);//第三個參數(shù)表示讀取出來的對象是否可以為null,默認(rèn)不為null

清除數(shù)據(jù)

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

就是這么簡單~

同時還有一些簡化操作SharedPreferences讀寫任意數(shù)據(jù)的方法:

    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

文章標(biāo)題:AnyPref,可直接在SharedPreferences中保存對象實例的SharedPreferences工具類
標(biāo)題來源:http://www.muchs.cn/article2/pgdpoc.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供動態(tài)網(wǎng)站Google、云服務(wù)器、網(wǎng)站設(shè)計公司、企業(yè)網(wǎng)站制作

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)

外貿(mào)網(wǎng)站建設(shè)