| 注册
请输入搜索内容

热门搜索

Java Linux MySQL PHP JavaScript Hibernate jQuery Nginx

android 2.3 StrictMode 使用

0
Android Java C/C++ Go 14443 次浏览

ANR窗口产生的原因是多种多样的。程序的主线程因为IO读写或网络阻塞而导致被阻塞了,外部存储设备被独占了或系统负荷(load)过高(即不是自己编写的程序的问题,可能是系统或者其他第三方程序导致的问题),都有可能导致ANR窗口的出现。

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

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

public void 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如下:

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)

1个答案

0

那请问,怎么分析这个日志呢?什么样的情况才是异常呢?