Error Checking for android program optimization

Source: Internet
Author: User

1. StrictMode is a class introduced by android API 9, which helps developers find some problems in code. It can set some policies. When a program violates these policies, it will prompt developers. There are two main types of policies: thread-based and VM-based. In terms of thread policy, it can discover code with slow write speed when the UI thread reads and writes DISK, the UI thread accesses the network. In terms of VM policy, it can detect Activity memory leaks, SQL object memory leaks, and resources are not released, which can limit the maximum number of objects in a class. When a program violates these policies, it notifies developers, such as log, in some ways. The Code is as follows.

Public static void enableStrictMode () {if (Build. VERSION. SDK_INT> = Build. VERSION_CODES.GINGERBREAD) {StrictMode. threadPolicy. builder threadPolicyBuilder = // thread policy new StrictMode. threadPolicy. builder (). detectAll () // detects all rule violations. penaltyLog (); // if a policy violation is detected, print the log. For more information, see API StrictMode. vmPolicy. builder vmPolicyBuilder = // VM policy new StrictMode. vmPolicy. builder (). detectAll (). penaltyLog (); if (Build. VERSION. SDK_INT> = Build. VERSION_CODES.HONEYCOMB) {threadPolicyBuilder. penaltyFlashScreen (); // detects a violation of the policy, which is a flickering vmPolicyBuilder. setClassInstanceLimit (MyClass. class, 1); // only one instance of MyClass can be restricted here; otherwise, the policy is violated.} StrictMode. setThreadPolicy (threadPolicyBuilder. build (); StrictMode. setVmPolicy (vmPolicyBuilder. build ());}}

 

You can put it in Application. onCreate (), so that when the program is opened, it will take effect. Do not use it in the release version, which consumes a lot of performance.
if(debug){  <pre name="code" class="java"><span style="white-space:pre">    </span>enableStrictMode() ;</pre>}  <pre></pre>  <br>  <br>  

 

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.