android 2.3 StrictMode 使用

來源:互聯網
上載者:User

  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中添加,這樣就能在程式啟動的最初一刻進行監控了。

輸出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)


相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.