最新Android之支持多进程、多线程的TrayPreferences代替SharedPreferences 的总结

来源:互联网 发布:拍婚纱照 知乎 编辑:程序博客网 时间:2024/05/22 02:25

一、简单介绍TrayPreferences

1、介绍:

在github上看到了一个 android SharedPreferences 的替代方案Tray,学习了一下。特性基于ContentProvider的多线程的数据存储方案支持多线程、多进程

2、使用原因:

        在我们的项目里面有在service里面单独开了一根进程,所以项目就有2根进程,不同进程之间Application数据不共享,有的时候我们使用一般的SharedPreferences在这边进程保持了,然后到那边进程去拿有可能拿不到,所以今天就用支持多进程、多线的TrayPreferences代替Android的 SharedPreferences

使用方法:

第一步:导入类库    
下载地址:http://download.csdn.net/detail/u012372365/9620240
第二步:实现代码
package aa.taonanwateraffairs.tool;import android.content.Context;import net.grandcentrix.tray.TrayPreferences;/** * @author 康小岱 * @date 2016/9/1 16:23 * @email 2013252121@qq.com */public class AppPrefs {    private static final int VERSION = 1;    private static volatile TrayEMMPrefs  mPrefs;    //private static final Object PrefsSyncObject = new Object();    /**     * 继承TrayPreferences以修改模块名     */    private static class TrayEMMPrefs extends TrayPreferences {        public TrayEMMPrefs(Context context) {            super(context, context.getPackageName(), VERSION);        }    }    private AppPrefs(Context context) {    }    private static TrayEMMPrefs getPrefs(Context context) {        if (mPrefs == null) {            synchronized(AppPrefs.class) {                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();    }}

第三步:在AndroidManifest.xml配置
代码如下:
  <provider            android:name="net.grandcentrix.tray.provider.TrayContentProvider"            android:authorities="com.sangfor.vpn.client.awork.tray"/>

第四步:具体调用
如果我们需要保持一个值为 int类型数据
AppPrefs.putSharedInt(this,"chenyu",1);
取值
int   a = App.getSharedInt(this,"chenyu",0);


1 0