严苛模式

参考网站

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 

detectAll 检测所有潜在的违例

detectCustomSlowCalls 自定义耗时操作

detectDiskReads 读磁盘

detectDiskWrites 写磁盘

detectNetwork 检查网络

detectResourceMismatches 检查资源类型是否匹配

· VmPolicy 

detectAll 检测所有潜在的

detectActivityLeaks 检测Activity的泄露

detectCleartextNetwork 检测明文的网络

detectFileUriExposure 检测file://或者是content://

detectLeakedClosableObjects 检查为管理的Closable对象

detectLeakedRegistrationObjects 检测需要注册类型是否解注

detectLeakedSqlLiteObjects 检测sqlite对象,如cursors

3.2 检测到违规项之后的表现形式

· penaltyDeath crash,在所有表现形式最后运行,

· penaltyDeathOnNetwork crash,在所有值钱,必须调用detectNetwork去允许这个。

· penaltyDialog 弹出dialog

· penaltyDropBox 将日志吸入到dropbox中

· penaltyFlashScreen 屏幕闪烁

· penaltyLog log日志

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值