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)
上述代码在Contact4.0中的应用:
Contact的清单文件中:
1 。。。。。 2 。。。。。 3 4 <application 5 android:name="com.android.contacts.ContactsApplication" 6 android:label="@string/contactsList" 7 android:icon="@mipmap/ic_launcher_contacts" 8 android:taskAffinity="android.task.contacts" 9 android:hardwareAccelerated="true" 10 > 11 12 。。。。。 13 。。。。。
然后在ContactsApplication.java的onCreate()方法中,会启动这个,代码如下:
1 public void onCreate() { 2 super.onCreate(); 3 4 if (Log.isLoggable(Constants.PERFORMANCE_TAG, Log.DEBUG)) { 5 Log.d(Constants.PERFORMANCE_TAG, "ContactsApplication.onCreate start"); 6 } 7 8 // Priming caches to placate the StrictMode police 9 Context context = getApplicationContext(); 10 PreferenceManager.getDefaultSharedPreferences(context); 11 AccountTypeManager.getInstance(context); 12 if (ENABLE_FRAGMENT_LOG) FragmentManager.enableDebugLogging(true); 13 if (ENABLE_LOADER_LOG) LoaderManager.enableDebugLogging(true); 14 15 if (Log.isLoggable(Constants.STRICT_MODE_TAG, Log.DEBUG)) { 16 StrictMode.setThreadPolicy( 17 new StrictMode.ThreadPolicy.Builder().detectAll().penaltyLog().build()); 18 } 19 20 if (Log.isLoggable(Constants.PERFORMANCE_TAG, Log.DEBUG)) { 21 Log.d(Constants.PERFORMANCE_TAG, "ContactsApplication.onCreate finish"); 22 } 23 }
这样就可以在Contact应用启动的时候对整个应用有效果.