本文整理汇总了Java中android.content.Context.MODE_MULTI_PROCESS属性的典型用法代码示例。如果您正苦于以下问题:Java Context.MODE_MULTI_PROCESS属性的具体用法?Java Context.MODE_MULTI_PROCESS怎么用?Java Context.MODE_MULTI_PROCESS使用的例子?那么恭喜您, 这里精选的属性代码示例或许可以为您提供帮助。您也可以进一步了解该属性所在类android.content.Context
的用法示例。
在下文中一共展示了Context.MODE_MULTI_PROCESS属性的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: getSharedPreferences
public static SharedPreferences getSharedPreferences(Context context, String name, int mode) {
// 虽然官方在4.4的时候解决了ArrayIndexOutOfBoundsException的问,为了解决其他异常先用下这个类的实现;
// java.lang.ClassCastException: java.lang.Boolean cannot be cast to java.util.HashMap at com.android.internal.util.XmlUtils.readMapXml(XmlUtils.java:494)
if (Build.VERSION.SDK_INT > AndroidVersionCodes.N_MR1) { // 原本应该“>= 19”,为了解决崩溃看了所有版本SharedPreferences实现的源码,并做了反射调用时的适配;
return context.getSharedPreferences(name, mode);
} else {
SharedPreferencesImpl sp;
synchronized (SharedPreferencesImpl.class) {
if (sSharedPrefs == null) {
sSharedPrefs = new HashMap<String, HashMap<String, SharedPreferencesImpl>>();
}
if (mExecutor == null) {
mExecutor = new ThreadPoolExecutorHandleCrash(2, 2, 10 * 1000L, java.util.concurrent.TimeUnit.MILLISECONDS,
new java.util.concurrent.LinkedBlockingQueue<Runnable>(),
new PriorityThreadFactory(TAG, android.os.Process.THREAD_PRIORITY_DEFAULT));
mExecutor.allowCoreThreadTimeOut(true);
}
final String packageName = context.getPackageName();
HashMap<String, SharedPreferencesImpl> packagePrefs = sSharedPrefs.get(packageName);
if (packagePrefs == null) {
packagePrefs = new HashMap<String, SharedPreferencesImpl>();
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 (context.getApplicationInfo().targetSdkVersion <
Build.VERSION_CODES.KITKAT) {
if (name == null) {
name = "null";
}
}
sp = packagePrefs.get(name);
if (sp == null) {
// File prefsFile = getSharedPrefsFile(name);
File prefsFile = (File) ReflectUtils.invokeMethod(context, "getSharedPrefsFile", new Class[]{String.class}, name);
sp = new SharedPreferencesImpl(prefsFile, mode);
packagePrefs.put(name, sp);
return sp;
}
}
if ((mode & Context.MODE_MULTI_PROCESS) != 0 ||
context.getApplicationInfo().targetSdkVersion < 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;
}
}