参考网站
http://blog.csdn.net/brokge/article/details/8543145
http://blog.csdn.net/qq_21430549/article/details/51183103
1.什么是严苛模式
严苛模式是一个开发工具,能够检测程序中的违例,从而修复。最常用的地方就是主线程中disk的读写和network。目前能有两大策略,线程策略(ThreadPolicy)和Vm策略(VmPolicy)。
2.如何使用
在Application、Activity的onCreate的supper.onCreate之间配置。
StrictMode.setThreadPolicy(new StrictMode.ThreadPolicy.Builder()
.detectDiskReads()//读磁盘
.detectDiskWrites()//写磁盘
.penaltyDialog()//弹出dialog
.detectNetwork() // 这里可以替换为detectAll() 就包括了磁盘读写和网络I/O
.penaltyLog() //打印logcat,当然也可以定位到dropbox,通过文件保存相应的log
.build());
StrictMode.setVmPolicy(new StrictMode.VmPolicy.Builder()
.detectLeakedSqlLiteObjects() //检测SQLite数据库对象
.penaltyLog() //打印logcat
.penaltyDeath()//一旦StrictMode消息被写到LogCat后应用就会崩溃。
.build());
3.如何配置
我们可以通过建造者模式去灵活配置。
3.1 检测项
· ThreadPolicy
o detectAll 检测所有潜在的违例
o detectCustomSlowCalls 自定义耗时操作
o detectDiskReads 读磁盘
o detectDiskWrites 写磁盘
o detectNetwork 检查网络
o detectResourceMismatches 检查资源类型是否匹配
· VmPolicy
o detectAll 检测所有潜在的
o detectActivityLeaks 检测Activity的泄露
o detectCleartextNetwork 检测明文的网络
o detectFileUriExposure 检测file://或者是content://
o detectLeakedClosableObjects 检查为管理的Closable对象
o detectLeakedRegistrationObjects 检测需要注册类型是否解注
o detectLeakedSqlLiteObjects 检测sqlite对象,如cursors
3.2 检测到违规项之后的表现形式
· penaltyDeath crash,在所有表现形式最后运行,
· penaltyDeathOnNetwork crash,在所有值钱,必须调用detectNetwork去允许这个。
· penaltyDialog 弹出dialog
· penaltyDropBox 将日志吸入到dropbox中
· penaltyFlashScreen 屏幕闪烁
· penaltyLog log日志