What is Strict mode?
As we know it’s not safe to perform a heavy operation on the main thread it will be a bad user experience and sometimes show ANR also. To prevent such things Strict mode will be helpful.
A strict mode is a developer tool that helps to identify usage of disk and network operation on the main thread, on which app is interacting with a user. It will help to prevent accidental usage of disk and network tasks on the main thread. Keeping disk and network operations on away from the main thread gives a smooth and responsive experience to the user.
It’s added from Android version 9. to enable strict mode on the application, we have to define a policy in Application or Activity before that let’s see what’s policy is.
What’s Strict Mode Policy?
The policy is nothing but an instruction to the app to detect which type of violation and what’s action need to taken when there is any violation happening on the app. There are two types of Policies.
- Thread Policy
- VM Policy → policy applied to all threads in the virtual machine’s process.
Thread Policy
Thread policy applied to a certain thread. Using the below code snip we can define the Thread policy and its penalty when a violation is detected.
StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder() .detectAll() .penaltyLog() .build(); StrictMode.setThreadPolicy(policy); |
We can customize the violation detection rule for ThreadPolicy. Some of the violation types in thread policy.
- detectCustomSlowCalls() → Detect the slow operation on main thread.
- detectDiskReads() → Detect disk read on main thread.
- detectDiskWrites() → Detect disk write on the main thread.
- detectNetwork() → Detect network operation on the main thread.
We can customize the penalty for the violation.
- penaltyDeath() → On detection of violation app will get crash.
- penaltyDeathOnNetwork() → on detection of network violation app will crash.
- penaltyDialog() → on detection of violation it app will show the dialog to the developer.
- penaltyLog() → on detection of violation app will print the logs in logcat.
Example for Strick Mode Thread Policy.
StrictMode.setThreadPolicy(new StrictMode.ThreadPolicy.Builder() .detectDiskReads() .detectDiskWrites() .detectNetwork() .penaltyLog() .build()); |
VM Policy
VM Policy is applied to all threads in the virtual machine’s process. Using the below code snip we can define the VM policy and its penalty when the violation is detected.
StrictMode.VmPolicy policy = new StrictMode.VmPolicy.Builder() .detectAll() .penaltyLog() .build(); StrictMode.setVmPolicy(policy); |
We can customize the violation detection rule for VM Policy. Some of the violation types in VM policy.
- detectActivityLeaks() → Detect memory leak.
- detectCleartextNetwork() → detect non secure network call.
- detectLeakedSqlLiteObjects() → Detect SQLite and SQLiteCursor without close it.
- detectUnsafeIntentLaunch() → detect an app trying to launch intent that is not generated within the app.
Same as Thread Policy we have different penalties based on the different violations.
- penaltyDeathOnCleartextNetwork() → on detection of unsecure network call app will get closed
Example for Strick Mode VM Policy.
StrictMode.setVmPolicy(new StrictMode.VmPolicy.Builder() .detectLeakedSqlLiteObjects() .detectLeakedClosableObjects() .penaltyDeath() .build()); |
At last, we are also able to detect violations within the app using a penaltyListener on Policy Builder.