当前位置:   article > 正文

深入理解SharedPrefences实现原理_java sharepreference 设计意图

java sharepreference 设计意图

SharedPreferences作为Android常用的持久化组件,很常用,在系统设置应用中就大量地被使用。相信大家基本都是用过,但是在使用过程中,大家是否又知道它的实现原理呢?

基本使用

 SharedPreferences使用很简单,比如我们要它保存某个字符串,在Activity使用如下:

/**
 * save number
 * @param num
 */
private void saveNumber(String num) {
   
    SharedPreferences sharedPreferences = getSharedPreferences("light_persist", MODE_PRIVATE);
    sharedPreferences.edit().putString("number", num).apply();
    //sharedPreferences.edit().putString("number", number).commit();
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10

上面代码执行后,会将变量num保存在/data/data/应用包名/shared_prefs/light_persist.xml。SharedPreferences使用很简单,但这并不表示它本身也简单,下面从三个角度来深入了解SharedPreferences的实现原理

对象初始化

 在上面的例子中,我们是在Activity通过调用getSharedPreferences()获取SharedPreferences对象,我们看下它的具体实现是怎样的。首先,我们要明白以下几点:

  • Activity继承了ContextThemeWrapper,ContextThemeWrapper继承了ContextWrapper,而ContextWrapper继承了抽象类Context,getSharedPreferences()为Context中的抽象方法;ContextWrapper重写了getSharedPreferences()方法,所以Activity中调用getSharedPreferences()方法,实际上调用的是ContextWrapper里面的方法。
  • ContextWrapper中getSharedPreferences()方法的实现是调用了Context对象mBase的getSharedPreferences()。mBase具体对象为ContextImpl,也就是说Context.getSharedPreferences()具体实现在ContextImpl中。

对于第一点,通过类的继承关系很容易知道;对于第二点,这个会涉及到Activity的启动过程,在ActivityThread的performLaunchActivity()方法中会初始化context,context初始化的时序图如下
context创建

Activity在启动的时候会创建ContextImpl对象,并调用Activity的attach()方法将ContextImpl对象传递下去,最终给ContextWrapper的Context对象mBase赋值。

在明白Activity中调用getSharedPreferences()方法实际上调用了CcontextImpl.getSharedPreferences()后,我们就很容易明白SharedPreferences对象是如何初始化的了。SharedPreferences对象初始化过程如下所示
SharedPreferences初始化
下面我们就看下SharedPreference初始化的代码实现

@Override
public SharedPreferences getSharedPreferences(String name, int mode) {
   
    // At least one application in the world actually passes in a null
    // name.  This happened to work because when we generated the file name
    // we would stringify it to "null.xml".  Nice.
    if (mPackageInfo.getApplicationInfo().targetSdkVersion <
            Build.VERSION_CODES.KITKAT) {
   
        if (name == null) {
   
            name = "null";
        }
    }

    File file;
    synchronized (ContextImpl.class) {
   
        if (mSharedPrefsPaths == null) {
   
            mSharedPrefsPaths = new ArrayMap<>();
        }
        file = mSharedPrefsPaths.get(name);
        //从缓存集合中取name对应的file,如果集合中没有,则根据name创建对应的file并添加进集合
        if (file == null) {
   
            file = getSharedPreferencesPath(name);
            mSharedPrefsPaths.put(name, file);
        }
    }
    return getSharedPreferences(file, mode);
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
@Override
public SharedPreferences getSharedPreferences(File file, int mode) {
   
    SharedPreferencesImpl sp;
    synchronized (ContextImpl.class) {
   
	    //getSharedPreferencesCacheLocked()根据应用包名从缓存集合中取ArrayMap集合
        final ArrayMap<File, SharedPreferencesImpl> cache = getSharedPreferencesCacheLocked();
        sp = cache.get(file);
        
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/代码秘术家/article/detail/60783
推荐阅读
相关标签
  

闽ICP备14008679号