androidAndroid 架构/模式Android

SharedProvider一个SharedPreference

2017-07-22  本文已影响259人  码农一颗颗

SharedProvider一个SharedPreferences 多进程解决方案,内部使用ContentProvider方式实现。

由于app采用了多进程的方式,踩了SharedPreferences的坑。所以决定用ContentProvider来实现一个自己的SharedPreferences。
github项目地址

坑1:SharedPreferences最大的坑就是多进程读取

描述问题:我在A进程中写入UserId,在B进程中读取UserId进行网络访问,有的时候会出现莫名其妙的问题,读取的UserId不是null就是上一次的UserId。
查阅官方文档:Android在api11的时候加入MODE_MULTI_PROCESS来支持多进程,但是在api23的时候废弃了这个flag,理由是在某些版本的android系统上,它无法可靠的运行,而且对于跨进程的并发修改没有提供任何机制来保证。应该使用ContentProvider来代替它进程多进程的数据修改。

坑2:多进程反复读写SharedPreferences

描述问题:我在A进程中反复写入SharedPreferences,在B进程中同时写入同一个name的SharedPreferences,导致B进程无法写入。
官方文档上描述:对于跨进程的并发修改没有提供任何机制来保证。

坑3:乱用SharedPreferences

这个我就不再这里写了,网上有大神写的非常详细点击这里

由于以后要调整架构采用组件化,每个组件需要读写一些app的公用信息例如登陆信息等,所以我做了扩展SharedProvider中除了基本信息还可以保存对象和集合

sharedProvider.edit().putObject(KEY_6, articleK, ArticleK.class.getName());
sharedProvider.edit().putList(KEY_7, articleKList);
sharedProvider.edit().putMap(KEY_8, kArticleVMap);
sharedProvider.edit().putSet(KEY_9, articleKSet);

综合以上问题所以决定用ContentProvider来实现一个自己的SharedPreferences。
项目地址点击这里

Maven

<dependency>
  <groupId>com.andrjhf.sharedprovider</groupId>
  <artifactId>sharedprovider-library</artifactId>
  <version>1.0.2</version>
  <type>pom</type>
</dependency>

Gradle via JCenter

compile 'com.andrjhf.sharedprovider:sharedprovider-library:1.0.2'

编译版本和最小支持版本

android {
    compileSdkVersion 23
    buildToolsVersion "23.0.1"
    
    defaultConfig {
        minSdkVersion 9
        targetSdkVersion 22
        ...
    }
}
接入方法:

AndroidManifest.xml中增加如下方法,标准的provider
android:authorities="com.andrjhf.sharedprovider"不能修改

 <provider
    android:name="com.andrjhf.sharedprovider.PreferencesContentProvider"
    android:authorities="com.andrjhf.sharedprovider"
    android:enabled="true"
    android:exported="false"></provider>
测试方法:
public class MainActivity extends Activity {
        public static final String TAG = "MainActivity";
        public static final String SHARED_NAME = "sharedName1";
        public static final String SHARED_NAME2 = "sharedName2";
        private static final String KEY_1 = "key1";
        private static final String KEY_2 = "key2";
        private static final String KEY_3 = "key3";
        private static final String KEY_4 = "key4";
        private static final String KEY_5 = "key5";
        private static final String KEY_6 = "key6";
        private static final String KEY_7 = "key7";
        private static final String KEY_8 = "key8";
        private static final String KEY_9 = "key9";
        private static final String KEY_10 = "key10";
        private static final String KEY_11 = "key11";
        private static final String KEY_12 = "key12";
        private static final String KEY_13 = "key13";
        private SharedProvider sharedProvider;
        private SharedProvider sharedProvider2;
        private int count = 0;
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            List<ArticleK> articleKList = new ArrayList<>();
            Map<ArticleK, ArticleV> kArticleVMap = new HashMap<>();
            Set<ArticleK> articleKSet = new HashSet<>();
            ArticleK articleK = new ArticleK();
            articleK.setTitle("标题");
            articleK.setDesc("我是一个简介");
            articleK.setImg("http://www.baidu.com/");
            articleK.setUrl("http://www.google.com/");
            articleKList.add(articleK);
            articleK = new ArticleK();
            articleK.setTitle("标题11111");
            articleK.setDesc("我是一个简介11111");
            articleK.setImg("http://www.baidu.com/11111");
            articleK.setUrl("http://www.google.com/11111");
            articleKList.add(articleK);
            ArticleV articleV = new ArticleV();
            articleK.setTitle("标题vvvvv");
            articleK.setDesc("我是一个简介vvvvv");
            articleK.setImg("http://www.baidu.com/vvvvv");
            articleK.setUrl("http://www.google.com/vvvvv");
            kArticleVMap.put(articleK, articleV);
            articleKSet.add(articleK);
            sharedProvider = new SharedProviderImpl(getApplicationContext(), SHARED_NAME);
            sharedProvider.edit().putString(KEY_1, "value1");
            sharedProvider.edit().putInt(KEY_2, 100);
            sharedProvider.edit().putLong(KEY_3, 100000000L);
            sharedProvider.edit().putFloat(KEY_4, 66.66F);
            sharedProvider.edit().putBoolean(KEY_5, false);
            sharedProvider.edit().putObject(KEY_6, articleK, ArticleK.class.getName());
            sharedProvider.edit().putList(KEY_7, articleKList);
            sharedProvider.edit().putMap(KEY_8, kArticleVMap);
            sharedProvider.edit().putSet(KEY_9, articleKSet);
            sharedProvider.edit().putString(KEY_10, "value10");
            sharedProvider.edit().remove(KEY_10);
    //        sharedProvider.edit().clear();
            sharedProvider2 = new SharedProviderImpl(getApplicationContext(), SHARED_NAME2);
            sharedProvider2.edit().putString(KEY_1, "value1");
            sharedProvider2.edit().putInt(KEY_2, 100);
            sharedProvider2.edit().putLong(KEY_3, 100000000L);
            sharedProvider2.edit().putFloat(KEY_4, 66.66F);
            sharedProvider2.edit().putBoolean(KEY_5, true);
            sharedProvider2.edit().putObject(KEY_6, articleK, ArticleK.class.getName());
            sharedProvider2.edit().putList(KEY_7, articleKList);
            sharedProvider2.edit().putMap(KEY_8, kArticleVMap);
            sharedProvider2.edit().putSet(KEY_9, articleKSet);
            sharedProvider2.edit().putString(KEY_10, "value10");
            Log.e(TAG, KEY_1 + " : " + sharedProvider2.getString(KEY_1, "default_key_1"));
            Log.e(TAG, KEY_2 + " : " + sharedProvider2.getInt(KEY_2, -100));
            Log.e(TAG, KEY_3 + " : " + sharedProvider2.getLong(KEY_3, -1000L));
            Log.e(TAG, KEY_4 + " : " + sharedProvider2.getFloat(KEY_4, -55.55F));
            Log.e(TAG, KEY_5 + " : " + sharedProvider2.getBoolean(KEY_5, false));
            Log.e(TAG, KEY_6 + " : " + sharedProvider2.getObject(KEY_6, ArticleK.class.getName()));
            Log.e(TAG, KEY_7 + " : " + sharedProvider2.getList(KEY_7));
            Log.e(TAG, KEY_8 + " : " + sharedProvider2.getMap(KEY_8));
            Log.e(TAG, KEY_9 + " : " + sharedProvider2.getSet(KEY_9));
            Log.e(TAG, KEY_10 + " : " + sharedProvider2.getString(KEY_10, "default10"));
            Log.e(TAG, "ALL" + " : " + sharedProvider2.getAll());
            //如果获取类型错误会返回默认值,没有类型转换
            Log.e(TAG, "Default " + KEY_2 + " : " + sharedProvider2.getLong(KEY_2, -1000L));
            Log.e(TAG, "Default " + KEY_3 + " : " + sharedProvider2.getInt(KEY_3, -100));
            sharedProvider2.contains(KEY_1);
    //        Intent intent = new Intent(MainActivity.this, MyService.class);
    //        startService(intent);
    //        new Thread(new Runnable() {
    //            @Override
    //            public void run() {
    //                while (true) {
    //                    try {
    //                        TimeUnit.MILLISECONDS.sleep(500);
    //                    } catch (InterruptedException e) {
    //                        e.printStackTrace();
    //                    }
    //                    int keyInt7 = sharedProvider.getInt(KEY_7, -1);
    //                    Log.e(TAG, KEY_7 + " : " + keyInt7);
    ////                    sharedProvider.edit().putInt(KEY_7, count++);
    //                }
    //            }
    //        }).start();
            new Thread(new Runnable() {
                @Override
                public void run() {
                    String string = getFromAssets("test.txt");
                    SharedPreferences sharedPreferences = getSharedPreferences("app", Context.MODE_PRIVATE);
                    long start = System.currentTimeMillis();
    //                sharedPreferences.edit().putString(string, "value").commit();
    //                Log.e(TAG, "set preferences end - start = " + (System.currentTimeMillis() - start));
    //
    //                start = System.currentTimeMillis();
    //                sharedProvider2.edit().putString(string, "value");
    //                Log.e(TAG, "set provider end - start = " + (System.currentTimeMillis() - start));
                    start = System.currentTimeMillis();
                    sharedPreferences.edit().putString("key", "1111111111111111111111111111111").commit();
                    Log.e(TAG, "set preferences end - start = " + (System.currentTimeMillis() - start));
                    start = System.currentTimeMillis();
                    sharedProvider2.edit().putString("key", "1111111111111111111111111111111");
                    Log.e(TAG, "set provider end - start = " + (System.currentTimeMillis() - start));
    //                start = System.currentTimeMillis();
    //                String valuepre = sharedPreferences.getString("key", "");
    //                Log.e(TAG, "get preferences end - start = " + (System.currentTimeMillis() - start));
    //
    //                start = System.currentTimeMillis();
    //                valuepre = sharedProvider2.getString("key", "");
    //                Log.e(TAG, "get provider end - start = " + (System.currentTimeMillis() - start));
                }
            }).start();
        }
        public void onClick(View view) {
            sharedProvider.edit().putString(KEY_8, "sdfsdfsdfsdf");
            Toast.makeText(this, KEY_8, Toast.LENGTH_SHORT).show();
        }
        public String getFromAssets(String fileName) {
            try {
                InputStreamReader inputReader = new InputStreamReader(getResources().getAssets().open(fileName));
                BufferedReader bufReader = new BufferedReader(inputReader);
                String line = "";
                String Result = "";
                while ((line = bufReader.readLine()) != null)
                    Result += line;
                return Result;
            } catch (Exception e) {
                e.printStackTrace();
            }
            return null;
        }
    }
缺陷:

ContentProvider多进程访问的时候有时会导致Client端进程被kill。
感谢@Shawn_Dut提出的问题。
例子:Service进程在写数据,Client进程在读取数据,如果Service进程由于各种原因挂掉了,Client进程有可能被kill,这个问题无法规避。
具体原因请点击这里QQ音乐技术团队的帖子

上一篇下一篇

猜你喜欢

热点阅读