安卓ssharedpreferences如何使用?sharedpreferences增删改查

实现对sharedpreferences的增删改查,封装在了一个类里,复制一下代码可以直接使用在你的项目组
使用:

    Data_SharedPreference   SP= new Data_SharedPreference(
                getApplicationContext(),
                "account",
                MODE_PRIVATE
        );
import android.content.Context;
import android.content.SharedPreferences;
import android.os.Build;
import java.util.Set;

public class Data_SharedPreference {
    private SharedPreferences sp;
    private SharedPreferences.Editor sp_Editor;
    private Context context;
    private String sp_Name;
    private Integer sp_Mode;

    /**
     * 实例化sp和spEditor
     * @param context  传入上下文
     * @param name_SP  传入保存SP对象生成的xml文件的名字
     * @param mode_SP  传入保存SP的类型,default为context.MODE_PRIVATE 内部私有不共享
     */
    public Data_SharedPreference(Context context,String name_SP,Integer mode_SP){
        this.sp_Name =name_SP;
        this.context=context;
        this.sp_Mode=mode_SP;

        if(sp_Mode == null){
            sp_Mode=  context.MODE_PRIVATE;
        }
        if(sp_Name ==null){
            sp_Name ="default";
        }
        if(sp ==null){
            sp=  context.getSharedPreferences(sp_Name,sp_Mode);
        }

        if(sp_Editor ==null){
            sp_Editor=   context.getSharedPreferences(sp_Name,sp_Mode).edit();
        }


    }

    /**
     * 判断类型分类并储存
     */
    public void putData(String key, Object object){
        if (object instanceof Integer) {
            int integer = ((Integer) object).intValue();
            sp_Editor.putInt(key,integer);
        }else if(object instanceof String){
            String string = ((String) object).toString();
            sp_Editor.putString(key,string);
        }else if(object instanceof Boolean){
            Boolean booleanValue = ((Boolean) object).booleanValue();
            sp_Editor.putBoolean(key,booleanValue);
        }else if(object instanceof Float){
            Float floatValut = ((float) object);
            sp_Editor.putFloat(key,floatValut);
        }else if(object instanceof Double){
            Double floatDouble=((Double)object).doubleValue();
            Float floatValue = new Float(floatDouble);
            sp_Editor.putFloat(key,floatValue);
        } else if(object instanceof Long){
            Long floatLong=((Long)object).longValue();
            sp_Editor.putLong(key,floatLong);
        }else if(object instanceof Set){
            Set setValue = (Set<String>)object;
            sp_Editor.putStringSet(key,setValue);
        }
        sp_Editor.commit();
    }

    /**
     * 根据key获取对应的value内容
     */
    public String getDataString(String key){
            return sp.getString(key,"error");
    }
    public Boolean getDataBoolean(String key){
        return sp.getBoolean(key,false);
    }
    public Float getDataFloat(String key){
        return sp.getFloat(key,-1);
    }
    public Integer getDataInteger(String key){
        return sp.getInt(key,-1);
    }
    public Long getDataLong(String key){
        return sp.getLong(key,-1);
    }
    public Set<String> getDataSetString(String key){
        return sp.getStringSet(key,null);
    }

    /**
     * 是否存在kay
     */

    public boolean isExistKey(String key){
        return sp.contains(key);
    }
    /**
     * 移除单个key储存的内容.成功返回true,不然false
     */
    public boolean deleteSingleKeyData(String key){
        if(sp_Editor !=null && isExistKey(key)) {
            sp_Editor.remove(key);
        }else{
            return false;
        }
        return true;
    }
    /**
     * 清空所有储存的内容,但是不删除生成的xml文件.成功返回true,不然false
     */
    public boolean deleteAllKeyData(String key){
        if(sp_Editor !=null) {
             sp_Editor.clear().commit();
        }else{
            return false;
        }
        return true;
    }

    /**
     * 清空所有储存的内容,同时删除生成的xml文件.成功返回true,不然false
     */
    public boolean deleteAllKeyDataAndFile(String key){
        if(sp_Editor !=null) {
            sp_Editor.clear().commit();
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
              if(context!=null)  context.deleteSharedPreferences(sp_Name);
            }
        }else{
            return false;
        }
        return true;
    }

    /**
     * 替换某个key储存的内容.成功返回true,不然false
     */
    public boolean replaceKeysValue(String key,Object object){
        if(sp !=null){
            putData(key,object);
        }else{
            return false;
        }
        return true;
    }



}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值