zoukankan      html  css  js  c++  java
  • SP的封装(数据持久化方式一)

    1.先看一段描述:

    Interface for accessing and modifying preference data returned by Context.getSharedPreferences(java.lang.String, int). For any particular set of preferences, there is a single instance of this class that all clients share. Modifications to the preferences must go through an SharedPreferences.Editor object to ensure the preference values remain in a consistent state and control when they are committed to storage

    这段描述讲的是sp(SharedPreferences实例)获取方式:Context.getSharedPreferences(java.lang.String, int),这个方法有两个参数,第一个表示sp对应xml的文件名,第二个为这个文件的模式,私有,共有,可读,可写

    修改sp需要使用编辑器:SharedPreferences.Editor,然修改后要提交:edit.commit

    2.这个sp实例为整个应用程序共享,从sp维护的xml文件中,可以存取各种类型数据

    3.先看看我封装SP工具类的代码,这个封装经过测试,可以很方便使用sp存取数据

    package com.market.sp;
    
    import android.content.Context;
    import android.content.SharedPreferences;
    
    import java.util.Set;
    
    import static android.R.id.edit;
    
    /**
     * 对SharedPreference的封装
     * 在包名目录下创建一个shared_pres目录,并维护一个config.xml文件
     * 所有数据的读取和存入都是对这个文件的操作
     * Created by Administrator on 2017/6/15.
     */
    
    public class SPUtils {
    
       private static  SharedPreferences sp = null;
    
        /**
         * 将一个boolean值存入sp文件中
         * @param ctx 上下文
         * @param key 存储节点名称
         * @param value 存储节点的值
         */
        public static void putBoolean(Context ctx, String key, boolean value){
            //如果sp为空,则获取创建一个sp对象
            if(sp == null){
                sp = ctx.getSharedPreferences("config",Context.MODE_PRIVATE);
            }
            sp.edit().putBoolean(key,value).commit();//获取sp编辑器,放入bool值,并提交
    
        }
    
        /**
         * 根据key读取一个boolean值value,没有的话使用defvalue代替
         * @param ctx
         * @param key
         * @param defvalue
         */
        public static boolean getBoolean(Context ctx, String key, boolean defvalue){
            //如果sp为空,则获取创建一个sp对象
            if(sp == null){
                sp = ctx.getSharedPreferences("config",Context.MODE_PRIVATE);
            }
            boolean b = sp.getBoolean(key, defvalue);
            return b;
    
        }
    
        /**
         * 将一个String值存入sp文件中
         * @param context 上下文
         * @param key 存储节点名称
         * @param value 存储节点的值
         */
        public static void putString(Context context,String key,String value){
            if(sp == null){//如果sp文件不存在,则创建该文件
                sp = context.getSharedPreferences("config", Context.MODE_PRIVATE);
            }
            sp.edit().putString(key, value).commit();
    
        }
        /**
         * 从sp中根据key取出String值
         * @param context 上下文
         * @param key 存储节点名称
         * @param defValue 存储节点默认值
         * @return string
         */
        public static String getString(Context context,String key,String defValue){
            if(sp == null){//如果sp文件不存在,则创建该文件
                sp = context.getSharedPreferences("config", Context.MODE_PRIVATE);
            }
            String string = sp.getString(key, defValue);
            return string;
    
        }
        /**
         * 移除sp中的一个节点
         * @param context 上下文环境
         * @param key 节点名称
         */
        public static void removeFromSP(Context context, String key) {
            if(sp == null){//如果sp文件不存在,则创建该文件
                sp = context.getSharedPreferences("config", Context.MODE_PRIVATE);
            }
            final SharedPreferences.Editor edit = sp.edit();
            edit.remove(key);
    
        }
        /**
         *  从sp中根据key取出int值
         * @param context
         * @param key
         * @param defValue
         * @return
         */
        public static int getInt(Context context, String key, int defValue) {
            if(sp == null){//如果sp文件不存在,则创建该文件
                sp = context.getSharedPreferences("config", Context.MODE_PRIVATE);
            }
            int i = sp.getInt(key, defValue);
            return i;
    
        }
        /**
         * 将一个int值存入sp文件中
         * @param context
         * @param key
         * @param value
         */
        public static void putInt(Context context,String key,int value){
            if(sp == null){//如果sp文件不存在,则创建该文件
                sp = context.getSharedPreferences("config", Context.MODE_PRIVATE);
            }
            sp.edit().putInt(key, value).commit();
    
        }
    
        /**
         *  从sp中根据key取出float值
         * @param context
         * @param key
         * @param defValue
         * @return
         */
        public static float getFloat(Context context, String key, float defValue) {
            if(sp == null){//如果sp文件不存在,则创建该文件
                sp = context.getSharedPreferences("config", Context.MODE_PRIVATE);
            }
            float i = sp.getFloat(key, defValue);
            return i;
    
        }
        /**
         * 将一个float值存入sp文件中
         * @param context
         * @param key
         * @param value
         */
        public static void putFloat(Context context,String key,float value){
            if(sp == null){//如果sp文件不存在,则创建该文件
                sp = context.getSharedPreferences("config", Context.MODE_PRIVATE);
            }
            sp.edit().putFloat(key,value).commit();
    
        }
    
        /**
         *  从sp中根据key取出int值
         * @param context
         * @param key
         * @param defValue
         * @return
         */
        public static Set<String> getStringSet(Context context, String key, Set<String> defValue) {
            if(sp == null){//如果sp文件不存在,则创建该文件
                sp = context.getSharedPreferences("config", Context.MODE_PRIVATE);
            }
            Set<String> sets = sp.getStringSet(key, defValue);
            return sets;
    
        }
        /**
         * 将一个int值存入sp文件中
         * @param context
         * @param key
         * @param sets
         */
        public static void putStringSet(Context context,String key,Set<String> sets){
            if(sp == null){//如果sp文件不存在,则创建该文件
                sp = context.getSharedPreferences("config", Context.MODE_PRIVATE);
            }
            sp.edit().putStringSet(key,sets).commit();
    
        }
    
    
    
    }

    4.测试代码:

    package com.market.sp;
    
    import android.app.Activity;
    import android.os.Bundle;
    import android.util.Log;
    
    public class MainActivity extends Activity {
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            SPUtils.putBoolean(this,"ismy",true);
            SPUtils.putFloat(this,"myfloat",23.45f);
    
            Log.e(getLocalClassName(),SPUtils.getBoolean(this,"ismy",false)+"");
            Log.e(getLocalClassName(),SPUtils.getFloat(this,"myfloat",0.0f)+"");
        }

    5.运行效果展示

    打印结果

    文件结果:可以看到在/data/data/com.market.sp/生成了shared_prefs目录,且在该目录下生成config.xml文件

    文件内容查看:可见存储了两个节点float和boolean,而且我们要存储的数据都在里面

    需要程序源码的可以加我微信x241602私聊。
  • 相关阅读:
    tomcat监控与优化
    rpm打包
    Rewrite和location 区别
    LNMP服务
    yum仓库脚本
    用户管理的脚本2
    pxe装机脚本
    用户管理的脚本1
    磁盘管理综合测试题
    MySQL 增量备份介绍及案例演示
  • 原文地址:https://www.cnblogs.com/huipengbo/p/7015557.html
Copyright © 2011-2022 走看看