多进程开发过程中sharedpreferences 数据共享问题的解决方案——TrayPreferences

来源:互联网 发布:jersey 接收post json 编辑:程序博客网 时间:2024/06/04 18:35

在开发过程中,应该有和我一样用到一个或多个service的多进程开发。然而SharedPreferences对多进程的支持不好, 你用什么mode也没用, 所以官方已经废弃了原先的MODE_MULTI_PROCESS, 并且建议跨进程存取值还是用ContentProvider之类的更靠谱一些。在github上看到了一个Android SharedPreferences 的替代方案Tray,学习了一下。特性基于ContentProvider的多线程的数据存储方案支持多线程、多进程。


1.首先下载资源jar包:http://download.csdn.net/detail/u011068702/9614526

2.添加一个适配器类

import android.content.Context;import net.grandcentrix.tray.TrayPreferences;/** * Created by wzl on 17-1-16. */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();    }}3.在AndroidManifest.xml配置
<provider    android:name="net.grandcentrix.tray.provider.TrayContentProvider"    android:authorities="com.sangfor.vpn.client.awork.tray"/>4.拿String类型为例,eg:
存值:AppPrefs.putSharedString(activity,Constant.LAST_VTALKIE, number);取值:AppPrefs.getSharedString(this,Constant.APPTOKEN)




1 0
原创粉丝点击