SharedPreferences的简单使用

自己写的,适合新手使用

1 定义一个接口

public interface ISharedPreferencesDao {
   /**
    * 
    * @param context
    * @param key
    * @param value
    */
   public void setStringParam(Context context, String key, String value);

   /**
    * 
    * @param context
    * @param key
    * @param value
    */
   public void setBooleanParam(Context context, String key, boolean value);

   /**
    * 
    * @param context
    * @param key
    * @param value
    */
   public void setIntParam(Context context, String key, int value);

   /**
    * 
    * @param context
    * @param key
    * @param value
    */
   public void setLongParam(Context context, String key, long value);

   /**
    *
    * @param context
    * @param key
    * @return
    */

   public String getStringParam(Context context, String key);

   /**
    *
    * @param context
    * @param key
    * @return
    */

   public boolean getBooleanParam(Context context, String key);

   /**
    *
    * @param context
    * @param key
    * @return
    */

   public int getIntParam(Context context, String key);

   /**
    *
    * @param context
    * @param key
    * @return
    */

   public long getLongParam(Context context, String key);

   /**
    *清除本地所有缓存的数据
    * @param context
    */

   public void cleanSharePreferences(Context context);

   /**
    *
    * @param context
    * @param key
    * @return  返回是不是含有指定的键
    */

   public boolean isContains(Context context, String key);

   /**
    * 存储一个对象文件
    * @param context
    * @param key
    * @param sharePrefencesModle
    */
   public void setModleParmas(Context context, String key, SharePrefencesModle sharePrefencesModle);

   /**
    * 获取存储的对象
    * @param context
    * @param key
    * @return
    */
   public SharePrefencesModle getModleParmas(Context context, String key);
}
 
2  实现这个接口
public class SharedPreferencesDaoImpl implements ISharedPreferencesDao {
	//这个是常量的名字
    private final String SharedPreferencesNAME = Constants.SHARED_PREFERNECES_NAME;
    private SharedPreferences sp = null;
    private Editor editor = null;

    @Override
    public void setStringParam(Context context, String key, String value) {
        initSpAndEd(context);
        editor.putString(key, value);
        editor.apply();
    }

    @Override
    public void setBooleanParam(Context context, String key, boolean value) {
        initSpAndEd(context);
        editor.putBoolean(key, value);
        editor.apply();
    }

    @Override
    public void setIntParam(Context context, String key, int value) {
        initSpAndEd(context);
        editor.putInt(key, value);
        editor.apply();
    }

    @Override
    public void setLongParam(Context context, String key, long value) {
        initSpAndEd(context);
        editor.putLong(key, value);
        editor.apply();
    }

    @Override
    public String getStringParam(Context context, String key) {
        initSp(context);
        return sp.getString(key, "");
    }

    @Override
    public boolean getBooleanParam(Context context, String key) {
        initSp(context);
        return sp.getBoolean(key, false);
    }

    @Override
    public int getIntParam(Context context, String key) {
        initSp(context);
        return sp.getInt(key, 0);
    }

    @Override
    public long getLongParam(Context context, String key) {
        initSp(context);
        return sp.getLong(key, 0l);
    }

    @Override
    public boolean isContains(Context context, String string) {
        initSp(context);
        return sp.contains(string);
    }


    @Override
    public void cleanSharePreferences(Context context) {
        initSpAndEd(context);
        editor.clear();
        editor.apply();
    }

    private void initSp(Context context) {
        if (sp == null) {
            sp = context.getSharedPreferences(SharedPreferencesNAME, MODE_PRIVATE);
        }
    }
    private void initEditor() {
        if (editor == null) {
            editor = sp.edit();
        }
    }
    private void initSpAndEd(Context context) {
        initSp(context);
        initEditor();
    }

    @Override
    public void setModleParmas(Context context, String key, SharePrefencesModle sharePrefencesModle) {
        initSpAndEd(context);
        try {
            ByteArrayOutputStream baos = new ByteArrayOutputStream();
            ObjectOutputStream oos = new ObjectOutputStream(baos);
            oos.writeObject(sharePrefencesModle);
            String base64Student = Base64.encodeToString(baos.toByteArray(), Base64.DEFAULT);
            editor.putString(key, base64Student);
            editor.apply();
            oos.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    @Override
    public SharePrefencesModle getModleParmas(Context context, String key) {
        initSp(context);
        String studentString = sp.getString(key, "");

        byte[] modelByte = Base64.decode(studentString, Base64.DEFAULT);

        ByteArrayInputStream bais = new ByteArrayInputStream(modelByte);

        try {

            ObjectInputStream ois = new ObjectInputStream(bais);

            SharePrefencesModle sharePrefencesModle = (SharePrefencesModle) ois.readObject();
            return sharePrefencesModle;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }
}
3 定义个一个工具类
public enum SharedPreferencesUtil {
    INSTRANSE;

    private ISharedPreferencesDao shareDao;

    public ISharedPreferencesDao getShareDao() {
        if (shareDao == null) {
            shareDao = new SharedPreferencesDaoImpl();
        }
        return shareDao;
    }
}
4 使用
SharedPreferencesUtil.INSTRANSE.getShareDao().getStringParam("作用域", "键名" );
SharedPreferencesUtil.INSTRANSE.getShareDao().setStringParam("作用域", "键名" , "键值" );

5 备注 
这个不适用在Application里面,在Activity或者Fragment里面都可以用. Service和Receiver里面没有测试过

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值