SharedPreferences 使用方法详解 Android 数据存储(一)

SharedPreferences是Android的一个接口类,是Android 数据存储(保存内部)的一种方法。主要以.xml 的形式保存在Android /data/data/com.**包名/shared_prefs下,SharedPreferences类提供了一个通用框架,以便用户能够保存和检索原始数据类型的键值对,原始数据类型如下:Boolean,Int,Float,Long,String。

欢迎关注微信公众号:程序员Android

微信公众号:ProgramAndroid

我们不是牛逼的程序员,我们只是程序开发中的垫脚石。

通过本章学习你将掌握以下知识点

1. SharedPreferences的使用方法
2. SharedPreferences保存数据的方法
3. SharedPreferences读取数据的方法
4. 总结SharedPreferencesUtils 封装类使用方法

1. SharedPreferences的使用方法

SharedPreferences 使用方法如下

1. 创建保存数据的xml文件
2. 使用Editor 向xml文件中保存数据
3. commit() 保存数据
4. xml保存地方
   /data/data/com.***包名/shared_prefs

2. SharedPreferences 保存数据的方法

主要使用 putBoolean() 和 putString() 等方法添加值。

3. SharedPreferences读取数据的方法

主要使用 getBoolean() 和 getString() 等 获取保存的数据

4. 总结SharePerference Utils 封装类使用方法

  • Utils 类如下:

public class SharePerferenceUtils {

    private static SharedPreferences sp;

    // 1,存储boolean变量方法
    public static void putBoolean(Context ctx, String key, boolean value) {
        // name存储文件名称
        if (sp == null) {
            sp = ctx.getSharedPreferences("config", Context.MODE_PRIVATE);
        }
        sp.edit().putBoolean(key, value).commit();
    }

    // 2,读取boolean变量方法
    public static boolean getBoolean(Context ctx, String key, boolean defValue) {
        // name存储文件名称
        if (sp == null) {
            sp = ctx.getSharedPreferences("config", Context.MODE_PRIVATE);
        }
        return sp.getBoolean(key, defValue);
    }

    public static void putString(Context ctx, String key, String value) {
        // name存储文件名称
        if (sp == null) {
            sp = ctx.getSharedPreferences("config", Context.MODE_PRIVATE);
        }
        sp.edit().putString(key, value).commit();
    }

    public static String getString(Context ctx, String key, String defValue) {
        // name存储文件名称
        if (sp == null) {
            sp = ctx.getSharedPreferences("config", Context.MODE_PRIVATE);
        }
        return sp.getString(key, defValue);
    }

    /**
     * @param ctx
     *            上下文环境
     * @param key
     *            要从config.xml移除节点的name的名称
     */
    public static void removeKey(Context ctx, String key) {
        if (sp == null) {
            sp = ctx.getSharedPreferences("config", Context.MODE_PRIVATE);
        }
        sp.edit().remove(key).commit();
    }

    // 反射(扩展)
    //
    public static void putInt(Context ctx, String key, int value) {
        // name存储文件名称
        if (sp == null) {
            sp = ctx.getSharedPreferences("config", Context.MODE_PRIVATE);
        }
        sp.edit().putInt(key, value).commit();
    }

    public static int getInt(Context ctx, String key, int defValue) {
        // name存储文件名称
        if (sp == null) {
            sp = ctx.getSharedPreferences("config", Context.MODE_PRIVATE);
        }
        return sp.getInt(key, defValue);
    }

}
  • Activity 类中使用方法如下:

  1. 保存数据

  1. 获取数据

至此 SharedPreferences的使用方法以基本完成。

注意:

欢迎关注微信公众号:程序员Android

微信公众号:ProgramAndroid

我们不是牛逼的程序员,我们只是程序开发中的垫脚石。

0?wx_fmt=gif

点击阅读原文,获取更多福利

0?wx_fmt=gif

posted @ 2017-09-26 13:56  程序员Android的博客  阅读(129)  评论(0编辑  收藏  举报