参考:Android严苛模式StrictMode使用详解
application的oncreate中添加:
// 分别为MainThread和VM设置Strict Mode
if (BuildConfig.DEBUG) {
StrictMode.setThreadPolicy(new StrictMode.ThreadPolicy.Builder()//线程策略检测
.detectCustomSlowCalls() //API等级11,自定义的耗时调用
// .detectDiskReads()//磁盘读取
// .detectDiskWrites()//磁盘写入
.detectNetwork() //网络
// .detectAll()//全检测
// .penaltyDialog() //弹出违规提示对话框
.penaltyLog() //在Logcat 中打印违规异常信息
// .detectResourceMismatches()//api23
// .penaltyDeath()//当触发违规条件时,直接Crash掉当前应用程序。
// .penaltyDropBox()//将违规信息记录到 dropbox 系统日志目录中(/data/system/dropbox),你可以通过如下命令进行插件:adb shell dumpsys dropbox dataappstrictmode --print
.penaltyFlashScreen() //API等级11,会造成屏幕闪烁,不过一般的设备可能没有这个功能。
.build())
StrictMode.setVmPolicy(new StrictMode.VmPolicy.Builder()//虚拟机策略检测
.detectLeakedSqlLiteObjects()//检测Sqlite对象是否关闭
.detectLeakedClosableObjects() //API等级11,检测未关闭的Closable对象
.detectActivityLeaks()//检测Activity内存泄露
// .detectLeakedRegistrationObjects()//api16 检查 BroadcastReceiver 或者ServiceConnection 注册类对象是否被正确释放
// .detectFileUriExposure()//api18
// .detectCleartextNetwork()//api23
// .detectAll()//全检测
.penaltyLog()
// .penaltyDeath()//当触发违规条件时,直接Crash掉当前应用程序
.build())
}
log 过滤:
StrictMode