Android || 本地缓存,SharedPreferences使用详解

王先生
2024-06-25 / 0 评论 / 14 阅读 / 正在检测是否收录...

SharedPreferences概述

SharedPreferences是Android平台上一个轻量级的存储辅助类,用来保存应用的一些常用配置。
它提供了String,set,int,long,float,boolean六种数据类型。
SharedPreferences的数据以键值对的进行保存在以xml形式的文件中。
在应用中通常做一些简单数据的持久化缓存。

SharedPreferences使用

获取 SharedPreferences

 //获取SharedPreferences对象
  SharedPreferences sharedPreferences = getSharedPreferences("appConfig",MODE_PRIVATE);

getSharedPreferences方法参数详解

  • name 存储文件名字
  • mode 存储方式 其值的含义如下

    说明
    Context.MODE_PRIVATE指定该SharedPreferences数据只能被本应用程序读、写
    Context.MODE_WORLD_READABLE指定该SharedPreferences数据能被其他应用程序读,但不能写
    Context.MODE_WORLD_WRITEABLE指定该SharedPreferences数据能被其他应用程序读
    Context.MODE_APPEND该模式会检查文件是否存在,存在就往文件追加内容,否则就创建新文件;

增加数据

 //获取SharedPreferences对象
  SharedPreferences sharedPreferences = getSharedPreferences("appConfig",MODE_PRIVATE);
 //获取Editor对象的引用
  SharedPreferences.Editor editor = sharedPreferences.edit();
 //将获取过来的值放入文件
  editor.putString("userAccount", "admin@1okk.com");
  editor.putString("userPassword", "admin");
  editor.putInt("age", 30);
  editor.putBoolean("islogin",true);
  // 提交数据
  editor.commit();

点击添加按钮以后我们可以看到在 data/data/ 应用程序包名的 shared_prefs 文件夹下生成了一个 appConfig.xml 的 xml 文件。点击可以打开该文件,可以看到该文件保存了如下的数据。

<?xml version='1.0' encoding='utf-8' standalone='yes' ?>
<map>
    <string name="userAccount">admin@1okk.com</string>
    <string name="userPassword">admin</string>
    <int name="age" value="30" />
    <boolean name="islogin" value="true" />
</map>

读取数据

读取数据可以通过获取SharedPreferences对象,通过 SharedPreferences 对象可以获取存储的数据值,第二个参数一般是个默认值,表示当获取数据的时候没有该key则返回一个默认值。

 //获取SharedPreferences对象
 SharedPreferences sharedPreferences= getSharedPreferences("appConfig", MODE_PRIVATE);
 String name=sharedPreferences.getString("userAccount","");
 int age = sharedPreferences.getInt("age",0);
 boolean islogin = sharedPreferences.getBoolean("islogin",true);
 Log.i("lucashu","name:"+ name +" age:" + age +" islogin:" + islogin);

删除数据

删除数据跟添加数据有点类似 也是通过Editor对象来完成。

//获取SharedPreferences对象
 SharedPreferences sharedPreferences = getSharedPreferences("appConfig",MODE_PRIVATE);
 //获取Editor对象的引用
 SharedPreferences.Editor editor = sharedPreferences.edit();
 //将获取过来的值放入文件
 editor.remove("userPassword");
 // 提交数据
 editor.commit();

修改数据

修改数据跟增加数据类似,覆盖原来的数据即修改数据。

 //获取SharedPreferences对象
  SharedPreferences sharedPreferences = getSharedPreferences("appConfig",MODE_PRIVATE);
 //获取Editor对象的引用
  SharedPreferences.Editor editor = sharedPreferences.edit();
 //将获取过来的值放入文件
  editor.putString("userAccount", "i@1okk.com");
  editor.putString("userPassword", "admin1");
  editor.putInt("age", 31);
  editor.putBoolean("islogin",false);
  // 提交数据
  editor.commit();

清除数据

我们可以通过Editor对象的clear方法来完成清楚数据。

//获取SharedPreferences对象
 SharedPreferences sharedPreferences = getSharedPreferences("appConfig", MODE_PRIVATE);
 //获取Editor对象的引用
 SharedPreferences.Editor editor = sharedPreferences.edit();
 editor.clear();
 // 提交数据
 editor.commit();

清除以后user.xml文件并不会删除,但里面的数据都没了

SharedPreferences封装类

package net.moao.handheldfixedpointwifi.utils.preferencesUtil;

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


/**
 * App 使用配置 轻量化存储 工具
 * @author Moao.Net
 * @DATE 2024/6/25
 */
public class AppConfig{
    public static String PREFERENCE_NAME = "appConfig";

    /**存储字符串*/
    public static boolean putString(Context context, String key, String value) {
        SharedPreferences preferences = context.getSharedPreferences(PREFERENCE_NAME, 0);
        SharedPreferences.Editor editor = preferences.edit();
        editor.putString(key, value);
        return editor.commit();
    }
    /**读取字符串*/
    public static String getString(Context context, String key) {
        return getString(context, key, null);
    }
    /**读取字符串(带默认值的)*/
    public static String getString(Context context, String key, String defaultValue) {
        SharedPreferences preferences = context.getSharedPreferences(PREFERENCE_NAME, 0);
        return preferences.getString(key, defaultValue);
    }
    /**存储整型数字*/
    public static boolean putInt(Context context, String key, int value) {
        SharedPreferences preferences = context.getSharedPreferences(PREFERENCE_NAME, 0);
        SharedPreferences.Editor editor = preferences.edit();
        editor.putInt(key, value);
        return editor.commit();
    }
    /**读取整型数字*/
    public static int getInt(Context context, String key) {
        return getInt(context, key, -1);
    }
    /**读取整型数字(带默认值的)*/
    public static int getInt(Context context, String key, int defaultValue) {
        SharedPreferences preferences = context.getSharedPreferences(PREFERENCE_NAME, 0);
        return preferences.getInt(key, defaultValue);
    }
    /**存储长整型数字*/
    public static boolean putLong(Context context, String key, long value) {
        SharedPreferences preferences = context.getSharedPreferences(PREFERENCE_NAME, 0);
        SharedPreferences.Editor editor = preferences.edit();
        editor.putLong(key, value);
        return editor.commit();
    }
    /**读取长整型数字*/
    public static long getLong(Context context, String key) {
        return getLong(context, key, 0xffffffff);
    }
    /**读取长整型数字(带默认值的)*/
    public static long getLong(Context context, String key, long defaultValue) {
        SharedPreferences preferences = context.getSharedPreferences(PREFERENCE_NAME, 0);
        return preferences.getLong(key, defaultValue);
    }
    /**存储Float数字*/
    public static boolean putFloat(Context context, String key, float value) {
        SharedPreferences preferences = context.getSharedPreferences(PREFERENCE_NAME, 0);
        SharedPreferences.Editor editor = preferences.edit();
        editor.putFloat(key, value);
        return editor.commit();
    }
    /**读取Float数字*/
    public static float getFloat(Context context, String key) {
        return getFloat(context, key, -1.0f);
    }
    /**读取Float数字(带默认值的)*/
    public static float getFloat(Context context, String key, float defaultValue) {
        SharedPreferences preferences = context.getSharedPreferences(PREFERENCE_NAME, 0);
        return preferences.getFloat(key, defaultValue);
    }
    /**存储boolean类型数据*/
    public static boolean putBoolean(Context context, String key, boolean value) {
        SharedPreferences preferences = context.getSharedPreferences(PREFERENCE_NAME, 0);
        SharedPreferences.Editor editor = preferences.edit();
        editor.putBoolean(key, value);
        return editor.commit();
    }
    /**读取boolean类型数据*/
    public static boolean getBoolean(Context context, String key) {
        return getBoolean(context, key, false);
    }
    /**读取boolean类型数据(带默认值的)*/
    public static boolean getBoolean(Context context, String key, boolean defaultValue) {
        SharedPreferences preferences = context.getSharedPreferences(PREFERENCE_NAME, 0);
        return preferences.getBoolean(key, defaultValue);
    }
    /**清除数据*/
    public static boolean clearPreferences(Context context) {
        SharedPreferences pref = context.getSharedPreferences(PREFERENCE_NAME, 0);
        SharedPreferences.Editor editor = pref.edit();
        editor.clear();
        return editor.commit();
    }
}

评论 (0)

取消