To improve your Android app performance, it's very important to protect the application's main thread as it is responsible for interacting with the user.
If we perform any kind of long-running operations such as IO operations on the main thread, the app freezes until that operation finishes. To help us identify these bottlenecks, Android SDK comes with the StrictMode.
The StrictMode helps us to detect undesirable operations and enforce penalties when one of those operations occurs. We can use the StrictMode during the development and testing phase of the application.
How to use the StrictMode class?
Using the StrictMode, we can build desired thread policy,. Thread policy includes what we want to detect and what we want the penalties to be? And then we set that policy as earlier as possible in the application. generally either at the application level or the start of the main activity. The policy stays in effect throughout the lifetime of the application. Make sure to remove these code before moving it to the production
Implementation:
Let gets started with the implementation.
First, we have to create a ThreadPolicy. We can start by creating a StrictMode.ThreadPolicy.Builder object and then calling methods like detectDiskReads() or detectDiskWrites() or detectNetwork(), and you can call as many detect methods as you wish to detect.
To detect all, you simply call detectAll() which will detect all of the disk operations, network operations, as well as a few other things that might happen on the main thread.
So now once we identify what we want the policy to detect, we can then decide what we want the penalty to be, and a policy can have multiple penalties.
StrictMode class provides the below penalties:
1. PenaltyLog: When any one of these operations are detected, write information out to the Logcat.
2. PenaltyException: If one of the operations occurs, go ahead and throw an exception.
3. PenaltyDialog: Displays a Dialog if one of the detected operations occurs. PenaltyDialog will show an annoying Dialog, trying to draw your attention to one of the operations that have occurred.
4. PenaltyDeath: Kill the process if one of these operations occurs.
And again, we can have multiple penalties. So if you set both penaltyLog and penaltyDeath in your policy, StrictMode will go ahead and write to the log before it kills the process. So you can be sure to capture the information that you want prior to the process death.
Now you can build the policy by calling the build method. Once the policy is created, we'll use the StrictMode class's static method, setThreadPolicy, and pass in the policy.
Code:
val policy : StrictMode.ThreadPolicy= StrictMode.ThreadPolicy.Builder().detectAll().penaltyLog().build()
StrictMode.setThreadPolicy(policy)
Now run your application. Whenever there is any StrictMode policy violation, you will be notified as per the specifications.
It is truly a well-researched content and excellent wording. I got so engaged in this material that I couldn’t wait to read. Read more info about App Development Company In Abu Dhabi. I am impressed with your work and skill. Thanks.
ReplyDeletePost a Comment