SharedPreferences工具类 - MvpSpUtils

概念

SharePrefences:是Androide自己提供的一个轻量级的存储工具,适用于存放一些少量的数据。

介绍使用

  • MvpSpUtils 用于读写sp文件
  • sp中getSharedperfences传入的参数的区别 ?

    MODE_PRIVATE:默认模式,这个个模式下,该应用只能访问自己创建的文件。再次提交类容时,会覆盖上一次提交的内容。
    MODE_APPEND:也是该应用只能访问自己创建的文件。但是再次提交文件时,会写在上一次提交文件的后面。
    MODE_WORLD_READABLE:会让其他应用读到该应用创建的文件。从API 17开始不推荐使用,是跨进程操作,不安全。
    MODE_WORLD_WRITEABLE:会让其他应用写入文本到该应用创建的文件中。也同MODE_WORLD_READABLE,不安全。

  • 文件名默认为 "mvp_sp_config"
  • saveApply saveCommit 保存
    commit:将会立马写入到文件中。
    apply:不会马上提交,先存入内存中。开了一个子线程,把值写入到文件中。
    在存入数据后立马读取的话,apply提交会有可能读不出来,commit提交可以读出来。
MvpSpUtils.saveApply(SP_VERSION_CODE,mApplication.getPackageManager().getPackageInfo(mApplication.getPackageName(), 0).versionCode);
  • remove 删除
 MvpSpUtils.remove(MvpManager.SP_VERSION_CODE);
  • getString getLong getInt 获取
MvpSpUtils.getString(MvpManager.SP_VERSION_CODE)

SharedPreferences工具类 - MvpSpUtils

package com.example.mvplib.utils;

import android.content.Context;
import android.content.SharedPreferences;

import com.example.mvplib.manager.MvpManager;


public class MvpSpUtils {

    private static final String   DEFAULT_SP_NAME = "mvp_sp_config";
    private static final int TYPE_COMMIT = 0X100;
    private static final int TYPE_APPLY = 0X101;


    public static String getString(String key){
       return getString(DEFAULT_SP_NAME, key);
    }

    public static String getString(String spName,String key){
        return MvpManager.getContext().getSharedPreferences(spName,Context.MODE_PRIVATE).getString(key,null);
    }


    public static long getLong(String key){
        return getLong(DEFAULT_SP_NAME, key);
    }

    public static long getLong(String spName,String key){
        return MvpManager.getContext().getSharedPreferences(spName,Context.MODE_PRIVATE).getLong(key,-1);
    }


    public static int getInt(String key){
        return getInt(DEFAULT_SP_NAME, key);
    }

    public static int getInt(String spName,String key){
        return MvpManager.getContext().getSharedPreferences(spName,Context.MODE_PRIVATE).getInt(key,-1);
    }

    public static void remove(String key){

        remove(DEFAULT_SP_NAME,key);
    }


    public static void remove(String spName,String key){
       SharedPreferences preferences =  MvpManager.getContext().getSharedPreferences(spName,Context.MODE_PRIVATE);

        SharedPreferences.Editor editor = preferences.edit();

        editor.remove(key);
        editor.commit();
    }


    public static void saveCommit(String key, Object value){
        saveString(DEFAULT_SP_NAME,key,value,TYPE_COMMIT);
    }


    public static void saveCommit(String spName, String key, Object value){
        saveString(spName,key,value,TYPE_COMMIT);
    }

    public static void saveApply(String spName, String key, Object value){
        saveString(spName,key,value,TYPE_APPLY);
    }

    public static void saveApply(String key, Object value){
        saveString(DEFAULT_SP_NAME,key,value,TYPE_APPLY);
    }

    private static void saveString(String spName,String key,Object value,int type){

        SharedPreferences preferences =  MvpManager.getContext().getSharedPreferences(spName,Context.MODE_PRIVATE);
        SharedPreferences.Editor editor = preferences.edit();

        if(value instanceof String){
            editor.putString(key,value.toString());
        }else if(value instanceof Float){
            editor.putFloat(key, (Float) value);
        }else if(value instanceof Integer){
            editor.putInt(key, (Integer) value);
        }else if(value instanceof Boolean){
            editor.putBoolean(key, (Boolean) value);
        }else if(value instanceof Long){
            editor.putLong(key, (Long) value);
        }

        if(type == TYPE_APPLY){
            editor.apply();
        }else{
            editor.commit();
        }
    }
}

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

友情链接更多精彩内容