当启动严格模式并设置android:persistent
为true
will 有错误。我该如何解决这个错误?谢谢。
应用
override fun onCreate() {
if (BuildConfig.DEBUG) {
setStrictMode()
}
super.onCreate()
}
private fun setStrictMode() {
StrictMode.setThreadPolicy(StrictMode.ThreadPolicy.Builder()
.detectDiskReads()
.detectDiskWrites()
.detectNetwork() // or .detectAll() for all detectable problems
.penaltyLog()
.build())
StrictMode.setVmPolicy(StrictMode.VmPolicy.Builder()
.detectLeakedSqlLiteObjects()
.detectLeakedClosableObjects()
.penaltyLog()
.penaltyDeath()
.build())
}
偏好示例.xml
<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<SwitchPreference
android:key="switch_preference_night_mode"
android:persistent="true"
android:title="@string/theme_settings_night_mode" />
</PreferenceScreen>
偏好片段:
public class ThemeSettingsFragment extends PreferenceFragmentCompat {
@Override
public void onCreatePreferences(Bundle savedInstanceState, String rootKey) {
setPreferencesFromResource(R.xml.preference_example, rootKey);
}
}
错误日志:
StrictMode: StrictMode policy violation; ~duration=30 ms: android.os.strictmode.DiskReadViolation
at android.os.StrictMode$AndroidBlockGuardPolicy.onReadFromDisk(StrictMode.java:1500)
at java.io.UnixFileSystem.checkAccess(UnixFileSystem.java:251)
at java.io.File.exists(File.java:815)
at android.app.ContextImpl.getDataDir(ContextImpl.java:2237)
at android.app.ContextImpl.getPreferencesDir(ContextImpl.java:550)
at android.app.ContextImpl.getSharedPreferencesPath(ContextImpl.java:747)
at android.app.ContextImpl.getSharedPreferences(ContextImpl.java:400)
at android.content.ContextWrapper.getSharedPreferences(ContextWrapper.java:174)
at android.content.ContextWrapper.getSharedPreferences(ContextWrapper.java:174)
at android.support.v7.preference.PreferenceManager.getSharedPreferences(PreferenceManager.java:330)