ANR窗口产生的原因是多种多样的。程序的主线程因为IO读写或网络阻塞而导致被阻塞了,外部存储设备被独占了或系统负荷(load)过高(即不是自己编写的程序的问题,可能是系统或者其他第三方程序导致的问题),都有可能导致ANR窗口的出现。
从Android 2.3开始提供了一个新的类StrictMode,可以帮助开发者改进他们的Android应用,StrictMode可以用于捕捉发生在应用程序主线程 中耗时的磁盘、网络访问或函数调用,可以帮助开发者使其改进程序,使主线程处理UI和动画在磁盘读写和网络操作时变得更平滑,
避免主线程被阻塞,导致ANR窗口的发生。
下面简要说明下Android 2.3新特性StrictMode限制模式的工作方式,见下面的代码:
01 | public void onCreate() { |
03 | StrictMode.setThreadPolicy( new StrictMode.ThreadPolicy.Builder() |
09 | StrictMode.setVmPolicy( new StrictMode.VmPolicy.Builder() |
10 | .detectLeakedSqlLiteObjects() |
上述代码可以在Application的OnCreate中添加,这样就能在程序启动的最初一刻进行监控了。
输出log如下:
01 | 02 - 27 10 : 03 : 56.122 : DEBUG/StrictMode( 16210 ): StrictMode policy violation; ~duration= 696 ms: android.os.StrictMode$StrictModeDiskReadViolation: policy= 23 violation= 2 |
02 | 02 - 27 10 : 03 : 56.122 : DEBUG/StrictMode( 16210 ): at android.os.StrictMode$AndroidBlockGuardPolicy.onReadFromDisk(StrictMode.java: 745 ) |
03 | 02 - 27 10 : 03 : 56.122 : DEBUG/StrictMode( 16210 ): at dalvik.system.BlockGuard$WrappedFileSystem.open(BlockGuard.java: 228 ) |
04 | 02 - 27 10 : 03 : 56.122 : DEBUG/StrictMode( 16210 ): at java.io.FileOutputStream.<init>(FileOutputStream.java: 94 ) |
05 | 02 - 27 10 : 03 : 56.122 : DEBUG/StrictMode( 16210 ): at java.io.FileOutputStream.<init>(FileOutputStream.java: 66 ) |
06 | 02 - 27 10 : 03 : 56.122 : DEBUG/StrictMode( 16210 ): at java.io.FileWriter.<init>(FileWriter.java: 42 ) |
07 | 02 - 27 10 : 03 : 56.122 : DEBUG/StrictMode( 16210 ): at org.zelos.asm.main.writeFile(main.java: 30 ) |
08 | 02 - 27 10 : 03 : 56.122 : DEBUG/StrictMode( 16210 ): at org.zelos.asm.main.onCreate(main.java: 19 ) |
09 | 02 - 27 10 : 03 : 56.122 : DEBUG/StrictMode( 16210 ): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java: 1047 ) |
10 | 02 - 27 10 : 03 : 56.122 : DEBUG/StrictMode( 16210 ): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java: 1611 ) |
11 | 02 - 27 10 : 03 : 56.122 : DEBUG/StrictMode( 16210 ): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java: 1663 ) |
12 | 02 - 27 10 : 03 : 56.122 : DEBUG/StrictMode( 16210 ): at android.app.ActivityThread.access$ 1500 (ActivityThread.java: 117 ) |
13 | 02 - 27 10 : 03 : 56.122 : DEBUG/StrictMode( 16210 ): at android.app.ActivityThread$H.handleMessage(ActivityThread.java: 931 ) |
14 | 02 - 27 10 : 03 : 56.122 : DEBUG/StrictMode( 16210 ): at android.os.Handler.dispatchMessage(Handler.java: 99 ) |
15 | 02 - 27 10 : 03 : 56.122 : DEBUG/StrictMode( 16210 ): at android.os.Looper.loop(Looper.java: 123 ) |
16 | 02 - 27 10 : 03 : 56.122 : DEBUG/StrictMode( 16210 ): at android.app.ActivityThread.main(ActivityThread.java: 3683 ) |
17 | 02 - 27 10 : 03 : 56.122 : DEBUG/StrictMode( 16210 ): at java.lang.reflect.Method.invokeNative(Native Method) |
18 | 02 - 27 10 : 03 : 56.122 : DEBUG/StrictMode( 16210 ): at java.lang.reflect.Method.invoke(Method.java: 507 ) |
19 | 02 - 27 10 : 03 : 56.122 : DEBUG/StrictMode( 16210 ): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java: 839 ) |
20 | 02 - 27 10 : 03 : 56.122 : DEBUG/StrictMode( 16210 ): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java: 597 ) |
21 | 02 - 27 10 : 03 : 56.122 : DEBUG/StrictMode( 16210 ): at dalvik.system.NativeStart.main(Native Method) |
22 | 02 - 27 10 : 03 : 56.162 : DEBUG/StrictMode( 16210 ): StrictMode policy violation; ~duration= 619 ms: android.os.StrictMode$StrictModeDiskWriteViolation: policy= 23 violation= 1 |
23 | 02 - 27 10 : 03 : 56.162 : DEBUG/StrictMode( 16210 ): at android.os.StrictMode$AndroidBlockGuardPolicy.onWriteToDisk(StrictMode.java: 732 ) |
24 | 02 - 27 10 : 03 : 56.162 : DEBUG/StrictMode( 16210 ): at dalvik.system.BlockGuard$WrappedFileSystem.open(BlockGuard.java: 230 ) |
25 | 02 - 27 10 : 03 : 56.162 : DEBUG/StrictMode( 16210 ): at java.io.FileOutputStream.<init>(FileOutputStream.java: 94 ) |
26 | 02 - 27 10 : 03 : 56.162 : DEBUG/StrictMode( 16210 ): at java.io.FileOutputStream.<init>(FileOutputStream.java: 66 ) |
27 | 02 - 27 10 : 03 : 56.162 : DEBUG/StrictMode( 16210 ): at java.io.FileWriter.<init>(FileWriter.java: 42 ) |
28 | 02 - 27 10 : 03 : 56.162 : DEBUG/StrictMode( 16210 ): at org.zelos.asm.main.writeFile(main.java: 30 ) |
29 | 02 - 27 10 : 03 : 56.162 : DEBUG/StrictMode( 16210 ): at org.zelos.asm.main.onCreate(main.java: 19 ) |
30 | 02 - 27 10 : 03 : 56.162 : DEBUG/StrictMode( 16210 ): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java: 1047 ) |
31 | 02 - 27 10 : 03 : 56.162 : DEBUG/StrictMode( 16210 ): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java: 1611 ) |
32 | 02 - 27 10 : 03 : 56.162 : DEBUG/StrictMode( 16210 ): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java: 1663 ) |
33 | 02 - 27 10 : 03 : 56.162 : DEBUG/StrictMode( 16210 ): at android.app.ActivityThread.access$ 1500 (ActivityThread.java: 117 ) |
34 | 02 - 27 10 : 03 : 56.162 : DEBUG/StrictMode( 16210 ): at android.app.ActivityThread$H.handleMessage(ActivityThread.java: 931 ) |
35 | 02 - 27 10 : 03 : 56.162 : DEBUG/StrictMode( 16210 ): at android.os.Handler.dispatchMessage(Handler.java: 99 ) |
36 | 02 - 27 10 : 03 : 56.162 : DEBUG/StrictMode( 16210 ): at android.os.Looper.loop(Looper.java: 123 ) |
37 | 02 - 27 10 : 03 : 56.162 : DEBUG/StrictMode( 16210 ): at android.app.ActivityThread.main(ActivityThread.java: 3683 ) |
38 | 02 - 27 10 : 03 : 56.162 : DEBUG/StrictMode( 16210 ): at java.lang.reflect.Method.invokeNative(Native Method) |
39 | 02 - 27 10 : 03 : 56.162 : DEBUG/StrictMode( 16210 ): at java.lang.reflect.Method.invoke(Method.java: 507 ) |
40 | 02 - 27 10 : 03 : 56.162 : DEBUG/StrictMode( 16210 ): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java: 839 ) |
41 | 02 - 27 10 : 03 : 56.162 : DEBUG/StrictMode( 16210 ): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java: 597 ) |
42 | 02 - 27 10 : 03 : 56.162 : DEBUG/StrictMode( 16210 ): at dalvik.system.NativeStart.main(Native Method) |