Tech Blog

Android

Strict Mode in Android

By Jignesh Mundhava3 min read
← Back to blog

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.

Keep reading

You may be interested in

Browse all articles →
Android Download file with Data URL
July 23, 20241 min read

Android Download file with Data URL

What is a Data URL? Data URLs are the type of URL scheme that is used to embed usually small types of files. In this URL, inline files are prefixed with the data: MIME type. data:[<mediatype>][;base64],<data> To download files having Data URLs inside Android: Firstly, we need to get an extension of the file from […]

By Bansari PanditRead More →
Download the file with blob URL in Android WebView
July 23, 20242 min read

Download the file with blob URL in Android WebView

What is the Blob URL? Blob URLs can only be generated by the browser with use of internal mapping and used within the current document while it’s open which means it is valid for the same session (ie. the life of the page/document). The blob URL looks like this: blob:http://example.com/550e8400-e29b-41d4-a716-446655440000 Below is the sample HTML […]

By Bansari PanditRead More →
Key Terminologies in Android Enterprise
December 20, 20227 min read

Key Terminologies in Android Enterprise

Android Enterprise is a Google-led initiative to enable the use of Android devices and applications in a workspace. This program offers APIs and tools for developers to integrate support of Android devices into the EMM (enterprise mobility management) or UEM (Unified Endpoint Management) solutions. The following are the key terms associated with Android Enterprise. This […]

By Sandeep KadamRead More →