Android多进程线程安全SharedPreferences

https://github.com/jovezhougang/mpsp

原理

使用ContentProvider 来保证进程访问数据安全,使用读写锁来保证线程访问安全

测试

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        MPSharedPreferences mpSharedPreferences = new MPSharedPreferences(getApplicationContext(),
                getPackageName());
        mpSharedPreferences.registerOnSharedPreferenceChangeListener(new MPSharedPreferences.OnSharedPreferenceChangeListener() {
            @Override
            public void onSharedPreferenceChanged(MPSharedPreferences sharedPreferences,
                                                  String key) {
                System.out.println(key);
            }
        });
        mpSharedPreferences.putBoolean("boolean",true);
        mpSharedPreferences.putFloat("float",9.9f);
        mpSharedPreferences.putInt("int",10);
        mpSharedPreferences.putLong("long",System.currentTimeMillis());
        mpSharedPreferences.putString("string","hello world");

        Set<String> sets = new HashSet<>();
        sets.add("8888");
        sets.add("9999");
        sets.add("0000");
        mpSharedPreferences.putStringSet("stringSet",sets);
        System.out.println(mpSharedPreferences.getString("string",""));
        System.out.println(mpSharedPreferences.getBoolean("boolean",false));
        System.out.println(mpSharedPreferences.getFloat("float",0));
        System.out.println(mpSharedPreferences.getInt("int",0));
        System.out.println(mpSharedPreferences.getLong("long",0));
        System.out.println(mpSharedPreferences.getStringSet("stringSet",null));

        for (int i = 0;i < 10000;i++) {
            System.out.println("i == " + i);
            System.out.println(mpSharedPreferences.getAll());
        }
    }

实现

package com.jove.mpsp;

import android.content.ContentValues;
import android.content.Context;
import android.database.ContentObserver;
import android.database.Cursor;
import android.net.Uri;
import android.os.Handler;
import android.os.Looper;

import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

public class MPSharedPreferences {

    private HashSet<OnSharedPreferenceChangeListener> listeners = new HashSet<>();

    public interface OnSharedPreferenceChangeListener {
        void onSharedPreferenceChanged(final MPSharedPreferences sharedPreferences,
                                       final String key);
    }

    private String name;
    private Context context;

    private ContentObserver mContentObserver =
            new ContentObserver(new Handler(Looper.getMainLooper())) {
                @Override
                public void onChange(boolean selfChange, Uri uri) {
                    synchronized (MPSharedPreferences.this) {
                        for (final OnSharedPreferenceChangeListener listener : listeners) {
                            listener.onSharedPreferenceChanged(MPSharedPreferences.this
                                    , uri.getPathSegments().get(1));
                        }
                    }
                }
            };

    public MPSharedPreferences(@NonNull Context context, @NonNull final String name) {
        this.name = name;
        this.context = context;
        this.context.getContentResolver()
                .registerContentObserver(Uri.parse(String.format("content://com.jove.mpsp" +
                                ".provider/%s"
                        , name)), true, mContentObserver);
    }

    public Map<String, ?> getAll() {
        Cursor cursor = null;
        try {
            cursor = context.getContentResolver()
                    .query(Uri.parse(String.format("content://com.jove.mpsp.provider/%s/all/0"
                            , name)), null, null, null, null);
            if (null != cursor && cursor.moveToNext()) {
                final Map<String, Object> map = new HashMap<>(cursor.getColumnCount());
                for (int i = 0; i < cursor.getColumnCount(); i++) {
                    switch (cursor.getType(i)) {
                        case Cursor.FIELD_TYPE_BLOB:
                            map.put(cursor.getColumnName(i), cursor.getBlob(i));
                            break;
                        case Cursor.FIELD_TYPE_STRING:
                        case Cursor.FIELD_TYPE_NULL:
                            map.put(cursor.getColumnName(i), cursor.getString(i));
                            break;
                        case Cursor.FIELD_TYPE_INTEGER:
                            map.put(cursor.getColumnName(i), cursor.getInt(i));
                            if (Long.parseLong(cursor.getString(i)) != cursor.getInt(i)) {
                                map.put(cursor.getColumnName(i),
                                        Long.parseLong(cursor.getString(i)));
                            }
                            break;
                        case Cursor.FIELD_TYPE_FLOAT:
                            map.put(cursor.getColumnName(i), cursor.getFloat(i));
                            break;
                    }
                }
                return map;
            }
        } finally {
            if (null != cursor) {
                cursor.close();
            }
        }
        return null;
    }

    @Nullable
    public String getString(String key, @Nullable String defValue) {
        Cursor cursor = null;
        try {
            cursor = context.getContentResolver()
                    .query(Uri.parse(String.format("content://com.jove.mpsp.provider/%s/%s/1"
                            , name, key)), null, null, null, null);
            if (null != cursor && cursor.moveToNext()) {
                return cursor.getString(0);
            }
        } finally {
            if (null != cursor) {
                cursor.close();
            }
        }
        return defValue;
    }

    @Nullable
    public Set<String> getStringSet(String key, @Nullable Set<String> defValues) {
        Cursor cursor = null;
        try {
            cursor = context.getContentResolver()
                    .query(Uri.parse(String.format("content://com.jove.mpsp.provider/%s/%s/6"
                            , name, key)), null, null, null, null);
            if (null != cursor && cursor.moveToNext()) {
                final Set<String> sets = new HashSet<>();
                for (int i = 0; i < cursor.getColumnCount(); i++) {
                    sets.add(cursor.getString(i));
                }
                return sets;
            }
        } finally {
            if (null != cursor) {
                cursor.close();
            }
        }
        return defValues;
    }

    public int getInt(String key, int defValue) {
        Cursor cursor = null;
        try {
            cursor = context.getContentResolver()
                    .query(Uri.parse(String.format("content://com.jove.mpsp.provider/%s/%s/4"
                            , name, key)), null, null, null, null);
            if (null != cursor && cursor.moveToNext()) {
                return cursor.getInt(0);
            }
        } finally {
            if (null != cursor) {
                cursor.close();
            }
        }
        return defValue;
    }

    public long getLong(String key, long defValue) {
        Cursor cursor = null;
        try {
            cursor = context.getContentResolver()
                    .query(Uri.parse(String.format("content://com.jove.mpsp.provider/%s/%s/5"
                            , name, key)), null, null, null, null);
            if (null != cursor && cursor.moveToNext()) {
                return Long.parseLong(cursor.getString(0));
            }
        } finally {
            if (null != cursor) {
                cursor.close();
            }
        }
        return defValue;
    }

    public float getFloat(final String key, final float defValue) {
        Cursor cursor = null;
        try {
            cursor = context.getContentResolver()
                    .query(Uri.parse(String.format("content://com.jove.mpsp.provider/%s/%s/3"
                            , name, key)), null, null, null, null);
            if (null != cursor && cursor.moveToNext()) {
                return cursor.getFloat(0);
            }
        } finally {
            if (null != cursor) {
                cursor.close();
            }
        }
        return defValue;
    }

    public boolean getBoolean(final String key, final boolean defValue) {
        Cursor cursor = null;
        try {
            cursor = context.getContentResolver()
                    .query(Uri.parse(String.format("content://com.jove.mpsp.provider/%s/%s/2"
                            , name, key)), null, null, null, null);
            if (null != cursor && cursor.moveToNext()) {
                return 0 == cursor.getInt(0) ? false : true;
            }
        } finally {
            if (null != cursor) {
                cursor.close();
            }
        }
        return defValue;
    }

    public boolean contains(final String key) {
        Cursor cursor = null;
        try {
            cursor = context.getContentResolver()
                    .query(Uri.parse(String.format("content://com.jove.mpsp.provider/%s/%s/7"
                            , name, key)), null, null, null, null);
            if (null != cursor && cursor.moveToNext()) {
                return cursor.getInt(0) > 0;
            }
        } finally {
            if (null != cursor) {
                cursor.close();
            }
        }
        return false;
    }

    public synchronized void registerOnSharedPreferenceChangeListener(final @NonNull OnSharedPreferenceChangeListener listener) {
        listeners.add(listener);
    }

    public synchronized void unregisterOnSharedPreferenceChangeListener(final @NonNull OnSharedPreferenceChangeListener listener) {
        listeners.remove(listener);
    }

    public boolean putString(String key, @Nullable String value) {
        final ContentValues values = new ContentValues();
        values.put(key, value);
        final Uri uri = context.getContentResolver()
                .insert(Uri.parse(String.format("content://com.jove.mpsp.provider/%s/%s/1"
                        , name, key)), values);
        return null != uri;
    }

    public boolean putStringSet(final String key, final @Nullable Set<String> values) {
        final ContentValues contentValues = new ContentValues();
        int index = 0;
        for (final String value : values) {
            contentValues.put("k" + index, value);
            index++;
        }
        final Uri uri = context.getContentResolver()
                .insert(Uri.parse(String.format("content://com.jove.mpsp.provider/%s/%s/6"
                        , name, key)), contentValues);
        return null != uri;
    }

    public boolean putInt(String key, int value) {
        final ContentValues values = new ContentValues();
        values.put(key, value);
        final Uri uri = context.getContentResolver()
                .insert(Uri.parse(String.format("content://com.jove.mpsp.provider/%s/%s/4"
                        , name, key)), values);
        return null != uri;
    }

    public boolean putLong(String key, long value) {
        final ContentValues values = new ContentValues();
        values.put(key, value);
        final Uri uri = context.getContentResolver()
                .insert(Uri.parse(String.format("content://com.jove.mpsp.provider/%s/%s/5"
                        , name, key)), values);
        return null != uri;
    }

    public boolean putFloat(String key, float value) {
        final ContentValues values = new ContentValues();
        values.put(key, value);
        final Uri uri = context.getContentResolver()
                .insert(Uri.parse(String.format("content://com.jove.mpsp.provider/%s/%s/3"
                        , name, key)), values);
        return null != uri;
    }

    public boolean putBoolean(String key, boolean value) {
        final ContentValues values = new ContentValues();
        values.put(key, value);
        final Uri uri = context.getContentResolver()
                .insert(Uri.parse(String.format("content://com.jove.mpsp.provider/%s/%s/2"
                        , name, key)), values);
        return null != uri;
    }

    public boolean remove(final String key) {
        final int nums = context.getContentResolver()
                .delete(Uri.parse(String.format("content://com.jove.mpsp.provider/remove/%s/%s"
                        , name, key)), null, null);
        return nums > 0;
    }

    public boolean clear() {
        final int nums = context.getContentResolver()
                .delete(Uri.parse(String.format("content://com.jove.mpsp.provider/clear/%s"
                        , name)), null, null);
        return nums > 0;
    }
}
package com.jove.mpsp;

import android.content.ContentProvider;
import android.content.ContentValues;
import android.content.Context;
import android.content.SharedPreferences;
import android.content.UriMatcher;
import android.database.Cursor;
import android.database.MatrixCursor;
import android.net.Uri;

import java.io.File;
import java.lang.reflect.Field;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.locks.ReentrantReadWriteLock;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

public class MPContentProvider extends ContentProvider implements SharedPreferences.OnSharedPreferenceChangeListener {
    private static UriMatcher uriMatcher = new UriMatcher(UriMatcher.NO_MATCH);
    private ReentrantReadWriteLock lock = new ReentrantReadWriteLock();

    static {
        uriMatcher.addURI("com.jove.mpsp.provider", "remove/*/*", 0x666);
        uriMatcher.addURI("com.jove.mpsp.provider", "clear/*", 0x999);
        uriMatcher.addURI("com.jove.mpsp.provider", "*/*/#", 0x888);
    }

    @Override
    public boolean onCreate() {
        return true;
    }

    @Nullable
    @Override
    public Cursor query(@NonNull Uri uri, @Nullable String[] projection,
                        @Nullable String selection, @Nullable String[] selectionArgs,
                        @Nullable String sortOrder) {
        try {
            lock.readLock().lock();
            switch (uriMatcher.match(uri)) {
                case 0x888:
                    final List<String> paths = uri.getPathSegments();
                    final SharedPreferences sp = getContext()
                            .getSharedPreferences(paths.get(0), Context.MODE_PRIVATE);
                    final int action = Integer.parseInt(paths.get(2));
                    if (action == 0) {
                        final Map<String, ?> map = sp.getAll();
                        if (null != map && 0 != map.size()) {
                            final Object[] columnValues = new Object[map.size()];
                            final String[] columnNames = new String[map.size()];
                            int index = 0;
                            for (final String key : map.keySet()) {
                                columnNames[index] = key;
                                columnValues[index] = map.get(key);
                                index++;
                            }
                            final MatrixCursor cursor = new MatrixCursor(columnNames
                                    , 1);
                            cursor.addRow(columnValues);
                            return cursor;
                        }
                        return null;
                    } else if (action == 1) {
                        final String key = paths.get(1);
                        if (sp.contains(key)) {
                            final MatrixCursor cursor = new MatrixCursor(new String[]{"v"}
                                    , 1);
                            cursor.addRow(new Object[]{sp.getString(paths.get(1), "")});
                            return cursor;
                        }
                        return null;
                    } else if (action == 2) {
                        final String key = paths.get(1);
                        if (sp.contains(key)) {
                            final MatrixCursor cursor = new MatrixCursor(new String[]{"v"}
                                    , 1);
                            cursor.addRow(new Object[]{sp.getBoolean(paths.get(1), false) ? 1 : 0});
                            return cursor;
                        }
                        return null;
                    } else if (action == 3) {
                        final String key = paths.get(1);
                        if (sp.contains(key)) {
                            final MatrixCursor cursor = new MatrixCursor(new String[]{"v"}
                                    , 1);
                            cursor.addRow(new Object[]{sp.getFloat(paths.get(1), 0)});
                            return cursor;
                        }
                        return null;
                    } else if (action == 4) {
                        final String key = paths.get(1);
                        if (sp.contains(key)) {
                            final MatrixCursor cursor = new MatrixCursor(new String[]{"v"}
                                    , 1);
                            cursor.addRow(new Object[]{sp.getInt(paths.get(1), 0)});
                            return cursor;
                        }
                        return null;
                    } else if (action == 5) {
                        final String key = paths.get(1);
                        if (sp.contains(key)) {
                            final MatrixCursor cursor = new MatrixCursor(new String[]{"v"}
                                    , 1);
                            cursor.addRow(new Object[]{sp.getLong(paths.get(1), 0)});
                            return cursor;
                        }
                        return null;
                    } else if (action == 6) {
                        final String key = paths.get(1);
                        if (sp.contains(key)) {
                            final Set<String> sets = sp.getStringSet(key, null);
                            if (null != sets && 0 != sets.size()) {
                                final String[] columnNames = new String[sets.size()];
                                final Object[] columnValues = new Object[sets.size()];
                                int index = 0;
                                for (final String value : sets) {
                                    columnNames[index] = "v" + index;
                                    columnValues[index] = value;
                                    index++;
                                }
                                final MatrixCursor cursor = new MatrixCursor(columnNames
                                        , 1);
                                cursor.addRow(columnValues);
                                return cursor;
                            }
                        }
                        return null;
                    } else if (action == 7) {
                        final String key = paths.get(1);
                        if (sp.contains(key)) {
                            final MatrixCursor cursor = new MatrixCursor(new String[]{"v"}
                                    , 1);
                            cursor.addRow(new Object[]{1});
                            return cursor;
                        }
                        return null;
                    }
                    break;
            }
            return null;
        } finally {
            lock.readLock().unlock();
        }
    }

    @Nullable
    @Override
    public String getType(@NonNull Uri uri) {
        return null;
    }

    @Nullable
    @Override
    public Uri insert(@NonNull Uri uri, @Nullable ContentValues values) {
        try {
            lock.writeLock().lock();
            switch (uriMatcher.match(uri)) {
                case 0x888:
                    final List<String> paths = uri.getPathSegments();
                    final SharedPreferences sp = getContext()
                            .getSharedPreferences(paths.get(0), Context.MODE_PRIVATE);
                    sp.unregisterOnSharedPreferenceChangeListener(this);
                    sp.registerOnSharedPreferenceChangeListener(this);
                    final int action = Integer.parseInt(paths.get(2));
                    if (action == 1) {
                        final String key = paths.get(1);
                        if (sp.edit().putString(key, values.getAsString(key)).commit()) {
                            return uri;
                        }
                        return null;
                    } else if (action == 2) {
                        final String key = paths.get(1);
                        if (sp.edit().putBoolean(key, values.getAsBoolean(key)).commit()) {
                            return uri;
                        }
                        return null;
                    } else if (action == 3) {
                        final String key = paths.get(1);
                        if (sp.edit().putFloat(key, values.getAsFloat(key)).commit()) {
                            return uri;
                        }
                        return null;
                    } else if (action == 4) {
                        final String key = paths.get(1);
                        if (sp.edit().putInt(key, values.getAsInteger(key)).commit()) {
                            return uri;
                        }
                        return null;
                    } else if (action == 5) {
                        final String key = paths.get(1);
                        if (sp.edit().putLong(key, values.getAsLong(key)).commit()) {
                            return uri;
                        }
                        return null;
                    } else if (action == 6) {
                        final Set<String> sets = new HashSet<>();
                        for (final String key : values.keySet()) {
                            sets.add(values.getAsString(key));
                        }
                        final String key = paths.get(1);
                        if (sp.edit().putStringSet(key, sets).commit()) {
                            return uri;
                        }
                        return null;
                    }
                    break;
            }
            return null;
        } finally {
            lock.writeLock().unlock();
        }
    }

    @Override
    public int delete(@NonNull Uri uri, @Nullable String selection,
                      @Nullable String[] selectionArgs) {
        try {
            lock.writeLock().lock();
            final List<String> paths = uri.getPathSegments();
            final SharedPreferences sp = getContext()
                    .getSharedPreferences(paths.get(1), Context.MODE_PRIVATE);
            sp.unregisterOnSharedPreferenceChangeListener(this);
            sp.registerOnSharedPreferenceChangeListener(this);
            switch (uriMatcher.match(uri)) {
                case 0x666:
                    return sp.edit().remove(paths.get(2)).commit() ? 1 : 0;
                case 0x999:
                    return sp.edit().clear().commit() ? 1 : 0;
            }
        } finally {
            lock.writeLock().unlock();
        }
        return 0;
    }

    @Override
    public int update(@NonNull Uri uri, @Nullable ContentValues values
            , @Nullable String selection, @Nullable String[] selectionArgs) {
        return 0;
    }

    @Override
    public void onSharedPreferenceChanged(final SharedPreferences sharedPreferences,
                                          final String key) {
        try {
            final Field field = sharedPreferences.getClass().getDeclaredField("mFile");
            field.setAccessible(true);
            final File file = (File) field.get(sharedPreferences);
            getContext().getContentResolver()
                    .notifyChange(Uri.parse(String.format("content://com.jove.mpsp.provider/%s/%s"
                            , file.getName().replaceAll(".xml", ""), key)), null);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 204,189评论 6 478
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 85,577评论 2 381
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 150,857评论 0 337
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 54,703评论 1 276
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 63,705评论 5 366
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 48,620评论 1 281
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 37,995评论 3 396
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 36,656评论 0 258
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 40,898评论 1 298
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 35,639评论 2 321
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 37,720评论 1 330
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 33,395评论 4 319
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 38,982评论 3 307
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 29,953评论 0 19
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 31,195评论 1 260
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 44,907评论 2 349
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 42,472评论 2 342