Created
November 27, 2021 16:23
-
-
Save adityabhaskar/6ebbd4ec29b9fa8cb8edacf6939e4570 to your computer and use it in GitHub Desktop.
Access SharedPreferences without StrictMode warnings
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
private fun <T> withPrefs(context: Context, callback: SharedPreferences.() -> T): T { | |
val oldPolicy = StrictMode.allowThreadDiskWrites() | |
val output = context.applicationContext | |
.getSharedPreferences(BILLING_STORAGE_NAME, Context.MODE_PRIVATE) | |
.callback() | |
StrictMode.setThreadPolicy(oldPolicy) | |
return output | |
} | |
// Sample | |
fun getThatBool(context: Context, key: String): Boolean { | |
return withPrefs(context) { getBoolean(key, false) } | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment