android的StrictMode的作用以及在程序中的应用
转载地址: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();
}
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中添加,这样就能在程序启动的最初一刻进行监控了。
输出log如下:
1 -27 10:03:56.122: DEBUG/StrictMode(16210): StrictMode policy violation; ~duration=696 ms: android.os.StrictMode$StrictModeDiskReadViolation: policy=23 violation=2 2 -27 10:03:56.122: DEBUG/StrictMode(16210): at android.os.StrictMode$AndroidBlockGuardPolicy.onReadFromDisk(StrictMode.java:745) 3 -27 10:03:56.122: DEBUG/StrictMode(16210): at dalvik.system.BlockGuard$WrappedFileSystem.open(BlockGuard.java:228) 4 -27 10:03:56.122: DEBUG/StrictMode(16210): at java.io.FileOutputStream.<init>(FileOutputStream.java:94) 5 -27 10:03:56.122: DEBUG/StrictMode(16210): at java.io.FileOutputStream.<init>(FileOutputStream.java:66) 6 -27 10:03:56.122: DEBUG/StrictMode(16210): at java.io.FileWriter.<init>(FileWriter.java:42) 7 -27 10:03:56.122: DEBUG/StrictMode(16210): at org.zelos.asm.main.writeFile(main.java:30) 8 -27 10:03:56.122: DEBUG/StrictMode(16210): at org.zelos.asm.main.onCreate(main.java:19) 9 -27 10:03:56.122: DEBUG/StrictMode(16210): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047) 10 -27 10:03:56.122: DEBUG/StrictMode(16210): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1611) 11 -27 10:03:56.122: DEBUG/StrictMode(16210): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1663) 12 -27 10:03:56.122: DEBUG/StrictMode(16210): at android.app.ActivityThread.access$1500(ActivityThread.java:117) 13 -27 10:03:56.122: DEBUG/StrictMode(16210): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:931) 14 -27 10:03:56.122: DEBUG/StrictMode(16210): at android.os.Handler.dispatchMessage(Handler.java:99) 15 -27 10:03:56.122: DEBUG/StrictMode(16210): at android.os.Looper.loop(Looper.java:123) 16 -27 10:03:56.122: DEBUG/StrictMode(16210): at android.app.ActivityThread.main(ActivityThread.java:3683) 17 -27 10:03:56.122: DEBUG/StrictMode(16210): at java.lang.reflect.Method.invokeNative(Native Method) 18 -27 10:03:56.122: DEBUG/StrictMode(16210): at java.lang.reflect.Method.invoke(Method.java:507) 19 -27 10:03:56.122: DEBUG/StrictMode(16210): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839) 20 -27 10:03:56.122: DEBUG/StrictMode(16210): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597) 21 -27 10:03:56.122: DEBUG/StrictMode(16210): at dalvik.system.NativeStart.main(Native Method) 22 -27 10:03:56.162: DEBUG/StrictMode(16210): StrictMode policy violation; ~duration=619 ms: android.os.StrictMode$StrictModeDiskWriteViolation: policy=23 violation=1 23 -27 10:03:56.162: DEBUG/StrictMode(16210): at android.os.StrictMode$AndroidBlockGuardPolicy.onWriteToDisk(StrictMode.java:732) 24 -27 10:03:56.162: DEBUG/StrictMode(16210): at dalvik.system.BlockGuard$WrappedFileSystem.open(BlockGuard.java:230) 25 -27 10:03:56.162: DEBUG/StrictMode(16210): at java.io.FileOutputStream.<init>(FileOutputStream.java:94) 26 -27 10:03:56.162: DEBUG/StrictMode(16210): at java.io.FileOutputStream.<init>(FileOutputStream.java:66) 27 -27 10:03:56.162: DEBUG/StrictMode(16210): at java.io.FileWriter.<init>(FileWriter.java:42) 28 -27 10:03:56.162: DEBUG/StrictMode(16210): at org.zelos.asm.main.writeFile(main.java:30) 29 -27 10:03:56.162: DEBUG/StrictMode(16210): at org.zelos.asm.main.onCreate(main.java:19) 30 -27 10:03:56.162: DEBUG/StrictMode(16210): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047) 31 -27 10:03:56.162: DEBUG/StrictMode(16210): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1611) 32 -27 10:03:56.162: DEBUG/StrictMode(16210): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1663) 33 -27 10:03:56.162: DEBUG/StrictMode(16210): at android.app.ActivityThread.access$1500(ActivityThread.java:117) 34 -27 10:03:56.162: DEBUG/StrictMode(16210): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:931) 35 -27 10:03:56.162: DEBUG/StrictMode(16210): at android.os.Handler.dispatchMessage(Handler.java:99) 36 -27 10:03:56.162: DEBUG/StrictMode(16210): at android.os.Looper.loop(Looper.java:123) 37 -27 10:03:56.162: DEBUG/StrictMode(16210): at android.app.ActivityThread.main(ActivityThread.java:3683) 38 -27 10:03:56.162: DEBUG/StrictMode(16210): at java.lang.reflect.Method.invokeNative(Native Method) 39 -27 10:03:56.162: DEBUG/StrictMode(16210): at java.lang.reflect.Method.invoke(Method.java:507) 40 -27 10:03:56.162: DEBUG/StrictMode(16210): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839) 41 -27 10:03:56.162: DEBUG/StrictMode(16210): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597) 42 -27 10:03:56.162: DEBUG/StrictMode(16210): at dalvik.system.NativeStart.main(Native Method)