android—数据存储方式 SharePreferences

SHaredPreferences是一种轻型的数据存储方式,他的本质是基于XML文件存储key-value键值对数据,通常用存储一些简单的配置信息

 

SHaredPreferences对象本身只能获取数据而不支持存储和vigia,存储修改是通过editor对象实现

 

实现步骤

1,根据Context获取SharedPreferences对象

2.利用edit()方法获取Editor对象

3.通过Editor对象爱嗯存储key-value键值对数据

4.通过commit()方法提交数据

 

    private void test(Context mContext){

        SharedPreferences sp=mContext.getSharedPreferences("config", Context.MODE_PRIVATE);
        sp.getString("key","为获取到");

        SharedPreferences.Editor editor=sp.edit();

        editor.putString("key","value");

        editor.commit();
    }

 

 

然后我们在封装一下

package binglian.com.binglian_mohe.utils;

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

/**
 * sharedPreferences封装
 */
public class ShareUtils {

//    private void test(Context mContext){
//
//        SharedPreferences sp=mContext.getSharedPreferences("config", Context.MODE_PRIVATE);
//        sp.getString("key","为获取到");
//
//        SharedPreferences.Editor editor=sp.edit();
//
//        editor.putString("key","value");
//
//        editor.commit();
//    }


    public static final String Name="config";


    //键值
    public static void putString(Context mContext,String key,String value){
        SharedPreferences sp=mContext.getSharedPreferences(Name,Context.MODE_PRIVATE);
        sp.edit().putString(key,value).commit();
    }

    //键 默认值
    public static String getString(Context mContext,String key,String defValue){
        SharedPreferences sp=mContext.getSharedPreferences(Name,Context.MODE_PRIVATE);
        return sp.getString(key,defValue);
    }


    public static void putInt(Context mContext,String key,int value){
        SharedPreferences sp=mContext.getSharedPreferences(Name,Context.MODE_PRIVATE);
        sp.edit().putInt(key,value).commit();
    }


    public static int getInt(Context mContext,String key,int defValue){
        SharedPreferences sp=mContext.getSharedPreferences(Name,Context.MODE_PRIVATE);
        return sp.getInt(key,defValue);
    }

    public static void putBoolean(Context mContext,String key,Boolean value){
        SharedPreferences sp=mContext.getSharedPreferences(Name,Context.MODE_PRIVATE);
        sp.edit().putBoolean(key,value).commit();
    }


    public static boolean getBoolean(Context mContext,String key,boolean defValue){
        SharedPreferences sp=mContext.getSharedPreferences(Name,Context.MODE_PRIVATE);
        return sp.getBoolean(key,defValue);
    }


    //删除单个
    public static void deleShare(Context mContext,String key){
        SharedPreferences sp=mContext.getSharedPreferences(Name,Context.MODE_PRIVATE);
        sp.edit().remove(key).commit();
    }


    //删除全部
    public static void deleAll(Context mContext){
        SharedPreferences sp=mContext.getSharedPreferences(Name,Context.MODE_PRIVATE);
        sp.edit().clear().commit();
    }

}

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值