Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Override android strict mode policy when in android tests to allow calling network on main thread.Useful when you want to access mock web server address and you don't care about the threading.
import android.os.StrictMode
fun <T> withStrictModeThreadPolicy(threadPolicy: StrictMode.ThreadPolicy, block: () -> T): T {
val previousPolicy = StrictMode.getThreadPolicy()
StrictMode.setThreadPolicy(threadPolicy)
try {
return block()
} finally {
StrictMode.setThreadPolicy(previousPolicy)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.