android中getSharedPreferences方法实现原理
答案:2 悬赏:50 手机版
解决时间 2021-03-26 05:13
- 提问者网友:鐵馬踏冰河
- 2021-03-25 06:10
android中getSharedPreferences方法实现原理
最佳答案
- 五星知识达人网友:从此江山别
- 2021-03-25 06:58
Context的真正实现在ContextImpl这个类里,常用的Context其实都是这个类的实例,这个类访问属性是package,所以在Eclipse看不到这个类,要去android源码目录下找
贴出getSharedPreferences方法的实现源码
private static ArrayMap> sSharedPrefs; @Override
public SharedPreferences getSharedPreferences(String name, int mode) {
SharedPreferencesImpl sp;
synchronized (ContextImpl.class) {
if (sSharedPrefs == null) {
sSharedPrefs = new ArrayMap>();
}
final String packageName = getPackageName();
ArrayMap packagePrefs = sSharedPrefs.get(packageName);
if (packagePrefs == null) {
packagePrefs = new ArrayMap();
sSharedPrefs.put(packageName, packagePrefs);
}
// 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";
}
}
sp = packagePrefs.get(name);
if (sp == null) {
File prefsFile = getSharedPrefsFile(name);
sp = new SharedPreferencesImpl(prefsFile, mode);
packagePrefs.put(name, 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;
}实际上返回的也是SharedPreferences的实现类SharedPreferencesImpl实例,SharedPreferences内部的存储实现我就不废话了,找到SharedPreferencesImpl这个类读一下就明白了,这段不复杂
贴出getSharedPreferences方法的实现源码
private static ArrayMap
public SharedPreferences getSharedPreferences(String name, int mode) {
SharedPreferencesImpl sp;
synchronized (ContextImpl.class) {
if (sSharedPrefs == null) {
sSharedPrefs = new ArrayMap
}
final String packageName = getPackageName();
ArrayMap
if (packagePrefs == null) {
packagePrefs = new ArrayMap
sSharedPrefs.put(packageName, packagePrefs);
}
// 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";
}
}
sp = packagePrefs.get(name);
if (sp == null) {
File prefsFile = getSharedPrefsFile(name);
sp = new SharedPreferencesImpl(prefsFile, mode);
packagePrefs.put(name, 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;
}实际上返回的也是SharedPreferences的实现类SharedPreferencesImpl实例,SharedPreferences内部的存储实现我就不废话了,找到SharedPreferencesImpl这个类读一下就明白了,这段不复杂
全部回答
- 1楼网友:渊鱼
- 2021-03-25 07:47
ContextWrapper 看下这个。
我要举报
如以上问答信息为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
大家都在看
推荐资讯