StrictMode
个人使用理解:可以监视项目当中书写代码的不规范和不好的代码,监视程序运行情况,如果出现严重的问题,会有相应的对话框提示,和对应的log,安卓在2.3API就加入了该API,官方定义:http://developer.android.com/reference/android/os/StrictMode.html,
发现有这么好个开发工具为什么不用到自己的项目里面来了,举例,把该工具添加到自己的项目,以安防项目为例:添加步骤也很简单,官网说的:Example code to enable from early in your Application, Activity, or other application component's onCreate() method:也就是放到你程序最先初始化的onCreate() 方法里面,也就是我们自己的Application 里面初始化StrictMode ,如下代码: if (DEVELOPER_MODE) {
StrictMode.setThreadPolicy(new StrictMode.ThreadPolicy.Builder
()
.detectDiskReads()
.detectDiskWrites()
.detectNetwork() // or .detectAll() for all detectable problems
.penaltyLog()
.build());
StrictMode.setVmPolicy(new StrictMode.VmPolicy.Builder
()
.detectLeakedSqlLiteObjects()
.detectLeakedClosableObjects()
.penaltyLog()
.penaltyDeath()
.build());
}
StrictMode.ThreadPolicy.Builder
()
.detectDiskReads()
.detectDiskWrites()
.detectNetwork() // or .detectAll() for all detectable problems
.penaltyLog()
.build());
StrictMode.setVmPolicy(new StrictMode.VmPolicy.Builder
()
.detectLeakedSqlLiteObjects()
.detectLeakedClosableObjects()
.penaltyLog()
.penaltyDeath()
.build());
}官网解释如下:
public static void setThreadPolicy (StrictMode.ThreadPolicy policy)
Added in API level 9
Sets the policy for what actions on the current thread should be detected, as well as the penalty if such actions occur.
Internally this sets a thread-local variable which is propagated across cross-process IPC calls, meaning you can catch violations when a system service or another process accesses the disk or network on your behalf.
Parameters
policy the policy to put into place
个人理解:对API要求是在9以上也就是2.3以上,该策略就是对线程使用的一个检测,在内部,该设置是传播跨跨进程的IPC调用,这意味着你可以捕捉违规行为时,系统服务或其他进程访问代表您的磁盘或网络中的线程局部变量。
setVmPolicy:设置检测虚拟机,