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;
    }
}

 

posted @ 2016-03-04 15:13  随易来了  阅读(219)  评论(0编辑  收藏  举报