一、简单介绍SharedPreferences
SharedPreferences类,它是一个轻量级的存储类,特别适合用于保存软件配置参数。
SharedPreferences保存数据,其背后是用xml文件存放数据,文件存放在/data/data/<package name>/shared_prefs目录下:
一个简单的存储代码如下:
SharedPreferences sharedPreferences = getSharedPreferences("wujay", Context.MODE_PRIVATE); //私有数据
Editor editor = sharedPreferences.edit();//获取编辑器
editor.putString("name", "chenyu");
editor.putInt("age", 4);
editor.commit();//提交修改
生成的wujay.xml文件内容如下:
<?xml version='1.0' encoding='utf-8' standalone='yes' ?> <map> <string name="name">chenyu</string> <int name="age" value="4" /> </map>
分析以下几个方法:
1、getSharedPreferences(name,mode)
方法的第一个参数用于指定该文件的名称,名称不用带后缀,后缀会由Android自动加上;
方法的第二个参数指定文件的操作模式,共有四种操作模式。
四种操作模式分别为:
1. MODE_APPEND: 追加方式存储
2. MODE_PRIVATE: 私有方式存储,其他应用无法访问
3. MODE_WORLD_READABLE: 表示当前文件可以被其他应用读取
4. MODE_WORLD_WRITEABLE: 表示当前文件可以被其他应用写入
2、edit()方法获取editor对象
Editor editor = sharedPreferences.edit();
editor存储对象采用key-value键值对进行存放,editor.putString("name", "chenyu");
通过commit()方法提交数据
与之对应的获取数据的方法:
SharedPreferences share = getSharedPreferences("SharedPreferences",Activity.MODE_WORLD_READABLE); int i=share.getInt("i",0); String str=share.getString("str",""); boolean flag=share.getBoolean("flag",false);
getString()第二个参数为缺省值,如果preference中不存在该key,将返回缺省值
如果你想要删除通过SharedPreferences产生的文件,可以通过以下方法:
File file= new File("/data/data/"+getPackageName().toString()+"/shared_prefs","Activity.xml"); if(file.exists()){ file.delete(); Toast.makeText(TestActivity.this, "删除成功", Toast.LENGTH_LONG).show(); }
3、访问其他应用中的Preference
2017-07-092017-07-09
二、简单介绍TrayPreferences
1、介绍:
在github上看到了一个 Android SharedPreferences 的替代方案Tray,学习了一下。特性基于ContentProvider的多线程的数据存储方案支持多线程、多进程
2、使用原因:
在我们的项目里面有在service里面单独开了一根进程,所以项目就有2根进程,不同进程之间Application数据不共享,有的时候我们使用一般的SharedPreferences在这边进程保持了,然后到那边进程去拿有可能拿不到,所以今天就用支持多进程、多线的TrayPreferences代替Android的 SharedPreferences
3、简单使用:
我们先得导入Tray.jar这个包,猛戳这个下载连接Tray.jar
封装代码如下:
public class AppPrefs { /** * 继承TrayPreferences以修改模块名 */ private static class TrayEMMPrefs extends TrayPreferences { public TrayEMMPrefs(Context context) { super(context, MODULE, VERSION); } } private EMMPrefs(Context context) { } private static TrayEMMPrefs getPrefs(Context context) { if (mPrefs == null) { synchronized(PrefsSyncObject) { if (mPrefs == null) { mPrefs = new TrayEMMPrefs(context); } } } return mPrefs; } /** * 设置可被多个进程共享的Boolean值 */ public static void putSharedBoolean(Context context, String key, boolean value) { TrayEMMPrefs prefs = getPrefs(context); prefs.put(key,value); } /** * 设置可被多个进程共享的Int值 */ public static void putSharedInt(Context context, String key, int value) { TrayEMMPrefs prefs = getPrefs(context); prefs.put(key,value); } /** * 设置可被多个进程共享的Long值 */ public static void putSharedLong(Context context, String key, long value) { TrayEMMPrefs prefs = getPrefs(context); prefs.put(key,value); } /** * 设置可被多个进程共享的String值 */ public static void putSharedString(Context context, String key, String value) { TrayEMMPrefs prefs = getPrefs(context); prefs.put(key,value); } /** * 获取可被多个进程共享的Boolean值,缺省值为false */ public static boolean getSharedBoolean(Context context, String key) { return getSharedBoolean(context, key, false); } /** * 获取可被多个进程共享的Boolean值,若key不存在,则返回defaultValue */ public static boolean getSharedBoolean(Context context, String key, boolean defaultValue) { TrayEMMPrefs prefs = getPrefs(context); return prefs.getBoolean(key, defaultValue); } /** * 获取可被多个进程共享的Int值,若key不存在,则返回0 */ public static int getSharedInt(Context context, String key) { return getSharedInt(context, key, 0); } /** * 获取可被多个进程共享的Int值,若key不存在,则返回defaultValue */ public static int getSharedInt(Context context, String key, int defaultValue) { TrayEMMPrefs prefs = getPrefs(context); return prefs.getInt(key, defaultValue); } /** * 获取可被多个进程共享的Long值,若key不存在,则返回0 */ public static long getSharedLong(Context context, String key) { return getSharedLong(context, key, 0); } /** * 获取可被多个进程共享的Long值,若key不存在,则返回defaultValue */ public static long getSharedLong(Context context, String key, long defaultValue) { TrayEMMPrefs prefs = getPrefs(context); return prefs.getLong(key, defaultValue); } /** * 获取可被多个进程共享的Int值,若key不存在,则返回null */ public static String getSharedString(Context context, String key) { return getSharedString(context, key, null); } /** * 获取可被多个进程共享的Int值,若key不存在,则返回defaultValue */ public static String getSharedString(Context context, String key, String defaultValue) { TrayEMMPrefs prefs = getPrefs(context); return prefs.getString(key, defaultValue); } public static void remove(Context context, String key) { TrayEMMPrefs prefs = getPrefs(context); if (key != null) { prefs.remove(key); } } /** * 清除配置文件 */ public static void clear(Context context) { TrayEMMPrefs prefs = getPrefs(context); prefs.clear(); } }
如果我们需要保持一个值为 int类型数据
AppPrefs.putSharedInt(this,"chenyu",1);
取值
int a = App.getSharedInt(this,"chenyu",0);
妈妈再也不担心由于多继承SharedPreferences 拿不到数据了。
github:https://github.com/grandcentrix/tray