• Shared Preferences 数据存储


    SharedPreferences类,它是一个轻量级的存储类,特别适合用于保存软件配置参数。

    其背后是用xml文件存放数据,文件存放在/data/data/<package name>/shared_prefs目录下,一个简单的存储代码如下:

    SharedPreferences sharedPreferences = getSharedPreferences("mysp", Context.MODE_PRIVATE); //私有数据

    在上述目录下生成mysp.xml文件

    如果你想要删除通过SharedPreferences产生的文件,可以通过以下方法:
    File file= new File("/data/data/"+getPackageName().toString()+"/shared_prefs","mysp.xml");
    if(file.exists())
    file.delete();

    public class SPUtil {
    
        /**
         * 获取sp对象
         * @param context
         * @return
         */
        public static SharedPreferences getSharedPreferences(Context context) {
            SharedPreferences sp = context.getSharedPreferences("config",
                    Context.MODE_PRIVATE);
            return sp;
        }
    
        /**
         * 从sp获取string值
         * @param context
         * @param key
         * @return
         */
        public static String getString(Context context, String key) {
            SharedPreferences sp = getSharedPreferences(context);
            String result = sp.getString(key, null);
            return result;
        }
    
        /**
         * 从sp获取int值
         * @param context
         * @param key
         * @return
         */
        public static int getInt(Context context, String key) {
            SharedPreferences sp = getSharedPreferences(context);
            int result = sp.getInt(key, 0);
            return result;
        }
    
        /**
         * 从sp获取boolean值
         * @param context
         * @param key
         * @return
         */
        public static boolean getBoolean(Context context, String key,
                boolean defaultValue) {
            SharedPreferences sp = getSharedPreferences(context);
            boolean result = sp.getBoolean(key, defaultValue);
            return result;
        }
    
        /**
         * 存储数据到sp中,仅限于string int boolean
         * @param context
         * @param key
         * @param value
         */
        public static void put(Context context, String key, Object value) {
            SharedPreferences sp = getSharedPreferences(context);
            Editor edit = sp.edit();
            if (value instanceof String) {
                edit.putString(key, (String) value);
            } else if (value instanceof Integer) {
                edit.putInt(key, (Integer) value);
            } else if (value instanceof Boolean) {
                edit.putBoolean(key, (Boolean) value);
            }
            edit.commit();
        }
    
        /**
         * 存储已读新闻id到sp中,
         * @param context
         * @param key
         * @param List<Integer> ids
         */
        public static void writeReadNews(Context context, String key,
                List<Integer> ids) {
            SharedPreferences sp = getSharedPreferences(context);
            Editor edit = sp.edit();
            StringBuffer sb = new StringBuffer();
            for (int i = 0; i < ids.size(); i++) {
                sb.append(ids.get(i) + ":");
            }
            // 去除最后一个":"
            if (sb.length() > 0) {
                sb.deleteCharAt(sb.length() - 1);
            }
            edit.putString(key, sb.toString());
            edit.commit();
        }
        
        /**
         * 存储显示新闻频道id到sp中,    
         * @param context
         * @param key
         * @param List<Integer> ids
         */
        public static void writeShowNewsChannel(Context context, String key,
                List<Integer> ids) {
            SharedPreferences sp = getSharedPreferences(context);
            Editor edit = sp.edit();
            StringBuffer sb = new StringBuffer();
            for (int i = 0; i < ids.size(); i++) {
                sb.append(ids.get(i) + ":");
            }
            // 去除最后一个":"
            if (sb.length() > 0) {
                sb.deleteCharAt(sb.length() - 1);
            }
            edit.putString(key, sb.toString());
            edit.commit();
        }
    
        /**
         * 从sp获取已读新闻id集合
         * 
         * @param context
         * @param key
         * @return ArrayList<Integer>
         */
        public static ArrayList<Integer> getReadNewsIds(Context context, String key) {
            SharedPreferences sp = getSharedPreferences(context);
            String ids = sp.getString(key, null);
            ArrayList<Integer> readIds = new ArrayList<Integer>();
            if (ids != null) {
                String[] split = ids.split(":");
                for (int i = 0; i < split.length; i++) {
                    readIds.add(Integer.valueOf(split[i]));
                }
            }
            return readIds;
        }
    }
  • 相关阅读:
    redisserver 双击闪退
    PHP QueryList采集器
    【ubuntu】配置国内源
    【ffmpeg基础知识】文件的删除和重命名
    【ffmpeg基础知识】打印视频meta信息
    Ubuntu下pkgconfig环境变量配置
    音视频基础知识
    【linux小技巧】返回上一个目录,vi默认显示行号,vi多窗口
    【ffmpeg基础知识】ffmpeg操作目录实现list
    【ffmpeg基础知识】日常日志
  • 原文地址:https://www.cnblogs.com/suiyilaile/p/5242186.html
Copyright © 2020-2023  润新知