Android StrictMode类使用实例

Android 2.3开始提供了一个新的类StrictMode,该类可以用于捕捉发生在应用程序主线程中耗时的磁盘、网络访问或函数调用,可以帮助开发者改进程序,使主线程处理UI和动画在磁盘读写和网络操作时变得更平滑,避免主线程被阻塞。

推荐博客:

http://www.cnblogs.com/zelos/archive/2011/02/27/1966403.html


ANR窗口产生的原因是多种多样的。程序的主线程因为IO读写或网络阻塞而导致被阻塞了,外部存储设备被独占了或系统负荷(load)过高

(即不是自己编写的程序的问题,可能是系统或者其他第三方程序导致的问题),都有可能导致ANR窗口的出现。

  从Android 2.3开始提供了一个新的类StrictMode,可以帮助开发者改进他们的Android应用,StrictMode可以用于捕捉发生在应用程序

主线程中耗时的磁盘、网络访问或函数调用,可以帮助开发者使其改进程序,使主线程处理UI和动画在磁盘读写和网络操作时变得更平滑,

避免主线程被阻塞,导致ANR窗口的发生。

 下面简要说明下Android 2.3新特性StrictMode限制模式的工作方式,见下面的代码:

复制代码
publicvoid onCreate() {
if (DEVELOPER_MODE) {
StrictMode.setThreadPolicy(new StrictMode.ThreadPolicy.Builder()
.detectDiskReads()
.detectDiskWrites()
.detectNetwork() // 这里可以替换为detectAll() 就包括了磁盘读写和网络I/O
.penaltyLog() //打印logcat,当然也可以定位到dropbox,通过文件保存相应的log
.build());
StrictMode.setVmPolicy(new StrictMode.VmPolicy.Builder()
.detectLeakedSqlLiteObjects() //探测SQLite数据库操作
.penaltyLog() //打印logcat
.penaltyDeath()
.build());
}
super.onCreate();
}
复制代码

上述代码可以在Application的OnCreate中添加,这样就能在程序启动的最初一刻进行监控了。

02-27 10:03:56.122: DEBUG/StrictMode(16210): StrictMode policy violation; ~duration=696 ms: android.os.StrictMode$StrictModeDiskReadViolation: policy=23 violation=2
02-27 10:03:56.122: DEBUG/StrictMode(16210):     at android.os.StrictMode$AndroidBlockGuardPolicy.onReadFromDisk(StrictMode.java:745)
02-27 10:03:56.122: DEBUG/StrictMode(16210):     at dalvik.system.BlockGuard$WrappedFileSystem.open(BlockGuard.java:228)
02-27 10:03:56.122: DEBUG/StrictMode(16210):     at java.io.FileOutputStream.<init>(FileOutputStream.java:94)
02-27 10:03:56.122: DEBUG/StrictMode(16210):     at java.io.FileOutputStream.<init>(FileOutputStream.java:66)
02-27 10:03:56.122: DEBUG/StrictMode(16210):     at java.io.FileWriter.<init>(FileWriter.java:42)
02-27 10:03:56.122: DEBUG/StrictMode(16210):     at org.zelos.asm.main.writeFile(main.java:30)
02-27 10:03:56.122: DEBUG/StrictMode(16210):     at org.zelos.asm.main.onCreate(main.java:19)
02-27 10:03:56.122: DEBUG/StrictMode(16210):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
02-27 10:03:56.122: DEBUG/StrictMode(16210):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1611)
02-27 10:03:56.122: DEBUG/StrictMode(16210):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1663)
02-27 10:03:56.122: DEBUG/StrictMode(16210):     at android.app.ActivityThread.access$1500(ActivityThread.java:117)
02-27 10:03:56.122: DEBUG/StrictMode(16210):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:931)
02-27 10:03:56.122: DEBUG/StrictMode(16210):     at android.os.Handler.dispatchMessage(Handler.java:99)
02-27 10:03:56.122: DEBUG/StrictMode(16210):     at android.os.Looper.loop(Looper.java:123)
02-27 10:03:56.122: DEBUG/StrictMode(16210):     at android.app.ActivityThread.main(ActivityThread.java:3683)
02-27 10:03:56.122: DEBUG/StrictMode(16210):     at java.lang.reflect.Method.invokeNative(Native Method)
02-27 10:03:56.122: DEBUG/StrictMode(16210):     at java.lang.reflect.Method.invoke(Method.java:507)
02-27 10:03:56.122: DEBUG/StrictMode(16210):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
02-27 10:03:56.122: DEBUG/StrictMode(16210):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
02-27 10:03:56.122: DEBUG/StrictMode(16210):     at dalvik.system.NativeStart.main(Native Method)
02-27 10:03:56.162: DEBUG/StrictMode(16210): StrictMode policy violation; ~duration=619 ms: android.os.StrictMode$StrictModeDiskWriteViolation: policy=23 violation=1
02-27 10:03:56.162: DEBUG/StrictMode(16210):     at android.os.StrictMode$AndroidBlockGuardPolicy.onWriteToDisk(StrictMode.java:732)
02-27 10:03:56.162: DEBUG/StrictMode(16210):     at dalvik.system.BlockGuard$WrappedFileSystem.open(BlockGuard.java:230)
02-27 10:03:56.162: DEBUG/StrictMode(16210):     at java.io.FileOutputStream.<init>(FileOutputStream.java:94)
02-27 10:03:56.162: DEBUG/StrictMode(16210):     at java.io.FileOutputStream.<init>(FileOutputStream.java:66)
02-27 10:03:56.162: DEBUG/StrictMode(16210):     at java.io.FileWriter.<init>(FileWriter.java:42)
02-27 10:03:56.162: DEBUG/StrictMode(16210):     at org.zelos.asm.main.writeFile(main.java:30)
02-27 10:03:56.162: DEBUG/StrictMode(16210):     at org.zelos.asm.main.onCreate(main.java:19)
02-27 10:03:56.162: DEBUG/StrictMode(16210):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
02-27 10:03:56.162: DEBUG/StrictMode(16210):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1611)
02-27 10:03:56.162: DEBUG/StrictMode(16210):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1663)
02-27 10:03:56.162: DEBUG/StrictMode(16210):     at android.app.ActivityThread.access$1500(ActivityThread.java:117)
02-27 10:03:56.162: DEBUG/StrictMode(16210):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:931)
02-27 10:03:56.162: DEBUG/StrictMode(16210):     at android.os.Handler.dispatchMessage(Handler.java:99)
02-27 10:03:56.162: DEBUG/StrictMode(16210):     at android.os.Looper.loop(Looper.java:123)
02-27 10:03:56.162: DEBUG/StrictMode(16210):     at android.app.ActivityThread.main(ActivityThread.java:3683)
02-27 10:03:56.162: DEBUG/StrictMode(16210):     at java.lang.reflect.Method.invokeNative(Native Method)
02-27 10:03:56.162: DEBUG/StrictMode(16210):     at java.lang.reflect.Method.invoke(Method.java:507)
02-27 10:03:56.162: DEBUG/StrictMode(16210):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
02-27 10:03:56.162: DEBUG/StrictMode(16210):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
02-27 10:03:56.162: DEBUG/StrictMode(16210):     at dalvik.system.NativeStart.main(Native Method)

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值