1. StrictMode是什么?

  StrictMode is a developer tool which detects things you might be doing by accident and brings them to your attention so you can fix them. 这个是Api文档的解释,意思就是为开发者在开发过程中提供的一个可以检测应用到应用中出现的问题并加以提示。使应用更加健壮,开发者也可以通过它来提高代码的水平,实现代码的一定优化,不是一股脑的写代码写完就完事。

2. StrictMode的使用

  StrictMode的使用,当然只是在调试中使用,真正上线版本就不能要了。所以和使用Log一样,设置一个全局的标志位DEBUG_TAG,当调试设置为true,上线设置为false。来控制代码的可用性。

if (DEBUG_FLAG) {
StrictMode.setThreadPolicy(
new StrictMode.ThreadPolicy.Builder()
.detectAll()
// .penaltyDialog() // 弹出违规提示对话框
.penaltyLog() // 在logcat中打印违规异常信息
.build());
StrictMode.setVmPolicy(
new StrictMode.VmPolicy.Builder()
.detectAll()
.penaltyLog()
.build());
}

  StrictMode策略:

  StrictMode.ThreadPolicy     StrictMode policy applied to a certain thread.

      关注点:Disk Reads 磁盘读  Disk Writes 磁盘写  Network access 网络访问  Custom Slow Code 自定义的运行速度慢的代码分析

  StrictMode.VmPolicy            StrictMode policy applied to all threads in the virtual machine's process.

      关注点:内存泄露的Activity对象  内存泄露的SQLite对象   内存泄露的释放的对象

3. StrictMode的版本适配

  StrictMode是Android2.3之后增加的一个类,所以对于Android之前是无法使用的,按照上面的代码,运行在Android2.3上肯定是有问题的。所以要做处理。处理方式可以通过异常捕获来处理。

4.参考:

Android最佳实践之:StrictMode介绍

严苛模式(StrictMode)

Android之StrictMode的更多相关文章

  1. Android的StrictMode

    转自:http://blog.csdn.net/tonyfield/article/details/8238251 Android 2.3提供一个称为严苛模式(StrictMode)的调试特性,Goo ...

  2. [Android Memory] Android 的 StrictMode

    android的2.3 之后引入的StrictMode 对网络的访问做了限制啊. public void onCreate() { if (DEVELOPER_MODE) { StrictMode.s ...

  3. Android StrictMode介绍

    转:http://www.blueowls.net/android-strictmode%E4%BB%8B%E7%BB%8D/ /** * enables "strict mode" ...

  4. 【转】 Pro Android学习笔记(五五):调试和分析(3):adb命令、模拟器控制台和StrictMode

    目录(?)[-] adb命令 模拟器Console StrictMode adb命令 我们在学习SQLite的使用,介绍过部分adb命令的使用,见Pro Android学习笔记(五):了解Conten ...

  5. Android严苛模式StrictMode使用详解

    StrictMode类是Android 2.3 (API 9)引入的一个工具类,可以用来帮助开发者发现代码中的一些不规范的问题,以达到提升应用响应能力的目的.举个例子来说,如果开发者在UI线程中进行了 ...

  6. Android内存泄露分析之StrictMode

    转载请注明地址:http://blog.csdn.NET/yincheng886337/article/details/50524709 StrictMode(严格模式)使用 StrictMode严格 ...

  7. Android开发之StrictMode

    StrictMode简介 Android开发者官网对StrictMode的介绍 StrictMode类是Android 2.3(API 9)引入的一个工具类 用来帮助开发者发现代码中一些不规范的问题, ...

  8. android 通过访问 php 接受 or 传送数据

    先说传送数据,可以在 利用 php 代替传送,直接把 访问的url加上 xxx.php?informatin=xxxxxx 就行了 接收的看代码吧,详细注释. 首先是 我自己定义的php 文件 < ...

  9. Android Studio实现APK的更新、下载、安装

    先不讲那么多看效果图: 下面来讲解一些更新CODE,原理大家都知道,不废话,直接上代码.里面有一些是我自己做的测试例子,所以大家可以直接删掉就好了 第一个:activity_main.xml < ...

随机推荐

  1. 使用URLConnection进行访问

    package test; import java.io.BufferedReader; import java.io.BufferedWriter; import java.io.IOExcepti ...

  2. log4j2笔记 #01# Architecture

    索引 Architecture Main Components Logger Hierarchy LoggerContext Configuration Logger LoggerConfig Fil ...

  3. Navicat使用ssh连接数据库

    1.主机名或ip地址必须填localhost 2.主机名或ip地址必须填外网ip地址

  4. P2698 [USACO12MAR]花盆Flowerpot(单调队列+二分)

    P2698 [USACO12MAR]花盆Flowerpot 一看标签........十分后悔 标签告诉你单调队列+二分了............ 每次二分花盆长度,蓝后开2个单调队列维护最大最小值 蓝 ...

  5. PyCharm 设置护眼背景色

    PyCharm 设置护眼背景色 一.方法 File -> Seting -> Editor -> Color Scheme -> General -> Text -> ...

  6. truncate table很慢之enq: RO - fast object reuse和local write wait等待分析

    使用ASSM表空间(默认模式)的时候,在dss系统中确实会出现truncate很慢的现象,但是他不会100%重现,得看概率.通过sql trace(对任何v$sysstat看起来资源消耗很低的情况,都 ...

  7. k8s device plugin

    基本概念入门: Device Manager Proposal Device plugin offical Doc(中文) device-plugins offical Doc(En) Go thro ...

  8. 什么是ip地址,什么是私有地址

    ip地址链接:https://jingyan.baidu.com/article/f96699bbf23089894e3c1be7.html 私有地址链接:https://baike.baidu.co ...

  9. Codeforces 825D Suitable Replacement - 贪心 - 二分答案

    You are given two strings s and t consisting of small Latin letters, string s can also contain '?' c ...

  10. PyCharm笔记之配色方案和取消波浪线

    转载:http://blog.csdn.net/xiemanr/article/details/72583718 转载:http://www.jb51.net/article/50689.htm 一. ...