启用直接启动模式的应用程序在实施工作管理器后崩溃?



由于后台服务的限制,我们在WorkManager的帮助下实现了我们的应用程序并替换了AsyncTask。该应用程序支持直接启动模式,因此我们将所需的SharedPreferences存储在设备加密存储中。应用程序在WorkManager实施之前工作正常,但现在重新启动设备后崩溃。 日志猫:

java.lang.IllegalStateException: SharedPreferences in credential encrypted storage are not available until after user is unlocked
at android.app.ContextImpl.getSharedPreferences(ContextImpl.java:387)
at android.app.ContextImpl.getSharedPreferences(ContextImpl.java:372)
at android.content.ContextWrapper.getSharedPreferences(ContextWrapper.java:167)
at androidx.work.impl.utils.Preferences.getSharedPreferences(Unknown Source:12)
at androidx.work.impl.utils.Preferences.needsReschedule(Unknown Source:0)
setNeedsReschedule
at androidx.work.impl.utils.ForceStopRunnable.shouldRescheduleWorkers(Unknown Source:6)
at androidx.work.impl.utils.ForceStopRunnable.run(Unknown Source:18)
at androidx.work.impl.utils.SerialExecutor$Task.run(Unknown Source:2)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1162)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:636)
at java.lang.Thread.run(Thread.java:764)

我们在初始化工作管理器时提供受设备保护的存储上下文:ContextCompat.createDeviceProtectedStorageContext(appContext); 请帮忙,如何处理。

这是与 - java.lang.IllegalStateException:凭据加密存储中的共享首选项在用户解锁之前不可用

如果您看到堆栈跟踪,它说,sharedpreferences 是凭据加密的,这意味着在用户解锁设备并且您提供的上下文用于设备加密存储之前,它将无法工作。

若要解决此问题,需要使用设备加密存储,而不是凭据加密存储。

请参阅此处的文档 - https://developer.android.com/training/articles/direct-boot

相关内容

最新更新