Oreo及更高版本:访问受保护存储上的共享首选项

时间:2018-12-19 15:17:54

标签: android

在用户升级后尝试重新启动应用程序服务时获取以下IllegalStateException(我们有一个升级接收器来检测此问题)->

java.lang.RuntimeException: 
  at android.app.ActivityThread.handleBindApplication (ActivityThread.java:5887)
  at android.app.ActivityThread.access$1100 (ActivityThread.java:200)
  at android.app.ActivityThread$H.handleMessage (ActivityThread.java:1651)
  at android.os.Handler.dispatchMessage (Handler.java:106)
  at android.os.Looper.loop (Looper.java:193)
  at android.app.ActivityThread.main (ActivityThread.java:6680)
  at java.lang.reflect.Method.invoke (Native Method)
  at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run (RuntimeInit.java:493)
  at com.android.internal.os.ZygoteInit.main (ZygoteInit.java:858)
Caused by: java.lang.IllegalStateException: 
  at android.app.ContextImpl.getSharedPreferences (ContextImpl.java:419)
  at android.app.ContextImpl.getSharedPreferences (ContextImpl.java:404)
  at android.content.ContextWrapper.getSharedPreferences (ContextWrapper.java:174)
  at com.xxx.xxx.utils.SharedPreferences.<init> (SharedPreferences.java:27)
  at com.xxx.xxx.utils.SharedPreferences.init (SharedPreferences.java:44)
  at com.xxx.xxx.MyApplication.onCreate (MyApplication.java:122)
  at android.app.Instrumentation.callApplicationOnCreate (Instrumentation.java:1154)
  at android.app.ActivityThread.handleBindApplication (ActivityThread.java:5882)

我们没有收到任何消息,但是它仅发生在一小部分设备上的Android Pie上,这使我们相信我们正在Android方面遇到此代码块(位于ContextImpl.java中):

@Override
    public SharedPreferences getSharedPreferences(File file, int mode) {
        SharedPreferencesImpl sp;
        synchronized (ContextImpl.class) {
            final ArrayMap<File, SharedPreferencesImpl> cache = getSharedPreferencesCacheLocked();
            sp = cache.get(file);
            if (sp == null) {
                checkMode(mode);
                if (getApplicationInfo().targetSdkVersion >= android.os.Build.VERSION_CODES.O) {
                    if (isCredentialProtectedStorage()
                            && !getSystemService(UserManager.class)
                                    .isUserUnlockingOrUnlocked(UserHandle.myUserId())) {
                        throw new IllegalStateException("SharedPreferences in credential encrypted "
                                + "storage are not available until after user is unlocked");
                    }
                }
                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;
    }

我的问题:

我们如何防止这种情况发生? “ isUserUnlockingOrUnlocked”是我们无法调用的系​​统方法。我们将如何进行检查并相应地解决此问题?

0 个答案:

没有答案