當前位置: 首頁>>代碼示例>>Java>>正文


Java Context.MODE_MULTI_PROCESS屬性代碼示例

本文整理匯總了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;
        }
    }
 
開發者ID:miLLlulei,項目名稱:Accessibility,代碼行數:54,代碼來源:SharedPreferencesImpl.java


注:本文中的android.content.Context.MODE_MULTI_PROCESS屬性示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。