Strict Mode in Android


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.

  1. Thread Policy
  2. 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.

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.

Same as Thread Policy we have different penalties based on the different violations.

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.

Leave A Comment

Your email address will not be published. Required fields are marked *