摘要:本文将带你了解Android应用开发android SharedPreference的实现(1),希望本文对大家学Android有所帮助。
本文将带你了解Android应用开发android SharedPreference的实现(1),希望本文对大家学Android有所帮助。
一直在用SharedPreferences进行数据缓存,也知道SharedPreferences使用xml文件保存缓存信息,但背后究竟是如何实现的?跟着源码一看究竟。
通常,我们在context.getSharedPreferences获得SharedPreferences,然后使用get方法根据键值获得值,使用SharedPreferences.edit获得Editor,向SharedPreferences中更键值。
追踪到最后,context.getSharedPreferences方法时在ContextImpl类中具体实现的。
?
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
<code><code><code><code><code><code>代码1
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);
if (file == null) {
file = getSharedPreferencesPath(name);
mSharedPrefsPaths.put(name, file);
}
}
return getSharedPreferences(file, mode);
}
</code></code></code></code></code></code>
可以看到,首先进行判断,如果targetSdkVersion小于19,如果传入的name为空,则创建null.xml文件,这会在后面讲解。接着会从mSharedPrefsPaths中根据传入的name去获取一个文件,这个文件其实就是xml的配置文件。看下mSharedPrefsPaths的申明:
?
1
2
<code><code><code><code><code><code><code>ArrayMap<string, file=""> mSharedPrefsPaths;
</string,></code></code></code></code></code></code></code>
从mSharedPrefsPaths的申明可以看出,mSharedPrefsPaths存储的是name和xml文件的映射,在getSharedPreferences会首先判断mSharedPrefsPaths是否会为空。mSharedPrefsPaths为空只会在应用启动后第一次调用getSharedPreferences才会发生,接着会从mSharedPrefsPaths中根据name获取一个xml配置文件,如果没有获取到xml配置文件,则调用getSharedPreferencesPath(name)创建一个xml配置文件。进入getSharedPreferencesPath看下里面发生了什么:
?
1
2
3
4
<code><code><code><code><code><code><code><code>public File getSharedPreferencesPath(String name) {
return makeFilename(getPreferencesDir(), name + ".xml");
}
</code></code></code></code></code></code></code></code>
好吧,终于看到这个配置文件是如何产生的了……,我就不解释这个位置了
再进入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
<code><code><code><code><code><code><code><code><code><code>代码2
public SharedPreferences getSharedPreferences(File file, int mode) {
checkMode(mode);
SharedPreferencesImpl sp;
synchronized (ContextImpl.class) {
final ArrayMap<file, sharedpreferencesimpl=""> cache = getSharedPreferencesCacheLocked();
sp = cache.get(file);
if (sp == null) {
sp = new SharedPreferencesImpl(file, mode);
cache.put(file, sp);
return sp;
}
}
if ((mode & Context.MODE_MULTI_PROCESS) != 0 ||
getApplicationInfo().targetSdkVersion < android.os.Build.VERSION_CODES.HONEYCOMB) {
// If somebody else (some other process) changed the prefs
// file behind our back, we reload it. This has been the
// historical (if undocumented) behavior.
sp.startReloadIfChangedUnexpectedly();
}
return sp;
}
</file,></code></code></code></code></code></code></code></code></code></code>
这里我们看到了SharedPreferencesImpl这样一个类。SharedPreferences实际上是一个接口,系统提供了SharedPreferencesImpl这样一个实现简化对SharedPreferences的操作。进入方法后,首先执行了
?
1
2
<code><code><code><code><code><code><code><code><code><code><code>ArrayMap<file, sharedpreferencesimpl=""> cache = getSharedPreferencesCacheLocked()
</file,></code></code></code></code></code></code></code></code></code></code></code>
可以猜测每个配置文件对应于一个SharedPreferencesImpl,后面我们会做具体的验证。再进入getSharedPreferencesCacheLocked() 方法体看下。
?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
<code><code><code><code><code><code><code><code><code><code><code><code>代码3
private ArrayMap<file, sharedpreferencesimpl=""> getSharedPreferencesCacheLocked() {
if (sSharedPrefsCache == null) {
sSharedPrefsCache = new ArrayMap<>();
}
final String packageName = getPackageName();
ArrayMap<file, sharedpreferencesimpl=""> packagePrefs = sSharedPrefsCache.get(packageName);
if (packagePrefs == null) {
packagePrefs = new ArrayMap<>();
sSharedPrefsCache.put(packageName, packagePrefs);
}
return packagePrefs;
}
</file,></file,></code></code></code></code></code></code></code></code></code></code></code></code>
进入方法体后,出现了sSharedPrefsCache这样一个变量,看下它的申明:
?
1
2
3
4
5
6
7
8
9
10
11
12
<code><code><code><code><code><code><code><code><code><code><code><code><code>private static ArrayMap<string, sharedpreferencesimpl="">> sSharedPrefsCache; 根据代码可知道,sSharedPrefsCache根据包名,存储了xml文件到SharedPreferencesImpl的映射。回到代码3,系统返回ArrayMap<file, sharedpreferencesimpl="">,并根据条件判断向sSharedPrefsCache添加ArrayMap<file, sharedpreferencesimpl="">。再回到代码2,获得到对应的缓存后,执行```sp = cache.get(file)```,如果对应的xml文件没有找到SharedPreferences,则为对应的xml文件新建SharedPreferencesImpl,并将新生成的SharedPreferencesImpl放入缓存中。我们进入SharedPreferencesImpl中看看是如何创建的。
代码4
SharedPreferencesImpl(File file, int mode) {
mFile = file;
mBackupFile = makeBackupFile(file);
mMode = mode;
mLoaded = false;
mMap = null;
startLoadFromDisk();
}
</file,></file,></string,></code></code></code></code></code></code></code></code></code></code></code></code></code>
代码4为SharedPreferencesImpl的构造函数,file为xml配置文件,mode为我们在调用getSharedPreferences方法时传入的mode的字段。可以看到,在构造对象时,系统首先执行makeBackupFile(file)创建了备份文件,并调用startLoadFromDisk()方法,startLoadFromDisk()方法实际上是读取file指向的xml配置文件,并将所有的配置对象读取到mMap中。好的,进入源码看下是不是这样:
?
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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
<code><code><code><code><code><code><code><code><code><code><code><code><code><code>代码5
private void startLoadFromDisk() {
synchronized (this) {
mLoaded = false;
}
new Thread("SharedPreferencesImpl-load") {
public void run() {
loadFromDisk();
}
}.start();
}
代码6
private void loadFromDisk() {
本文由职坐标整理并发布,希望对同学们有所帮助。了解更多详情请关注职坐标移动开发之Android频道!
您输入的评论内容中包含违禁敏感词
我知道了
请输入正确的手机号码
请输入正确的验证码
您今天的短信下发次数太多了,明天再试试吧!
我们会在第一时间安排职业规划师联系您!
您也可以联系我们的职业规划师咨询:
版权所有 职坐标-一站式IT培训就业服务领导者 沪ICP备13042190号-4
上海海同信息科技有限公司 Copyright ©2015 www.zhizuobiao.com,All Rights Reserved.
沪公网安备 31011502005948号