Android 2.3關於StrictMode使用教程

來源:互聯網
上載者:User

Android 2.3關於StrictMode使用是本文要介紹的內容,主要是來瞭解並學習Android 2.3的內容,具體關於Android 2.3內容的詳解來看本文。

ANR視窗產生的原因是多種多樣的。程式的主線程因為IO讀寫或網路阻塞而導致被阻塞了,外部存放裝置被獨佔了或系統負荷load)過高即不是自己編寫的程式的問題,可能是系統或者其他第三方程式導致的問題),都有可能導致ANR視窗的出現。

從Android 2.3開始提供了一個新的類StrictMode,可以協助開發人員改進他們的Android開發應用,StrictMode可以用於捕捉髮生在應用程式

主線程中耗時的磁碟、網路訪問或函數調用,可以協助開發人員使其改進程式,使主線程處理UI和動畫在磁碟讀寫和網路操作時變得更平滑,避免主線程被阻塞,導致ANR視窗的發生。

下面簡要說明下Android 2.3新特性StrictMode限制模式的工作方式,見下面的代碼:

 
  1. public void onCreate() {  
  2.   if (DEVELOPER_MODE) {  
  3.       StrictMode.setThreadPolicy(new StrictMode.ThreadPolicy.Builder()  
  4.               .detectDiskReads()  
  5.               .detectDiskWrites()  
  6.               .detectNetwork()   // 這裡可以替換為detectAll() 就包括了磁碟讀寫和網路I/O  
  7.               .penaltyLog()   //列印logcat,當然也可以定位到dropbox,通過檔案儲存相應的log  
  8.               .build());  
  9.       StrictMode.setVmPolicy(new StrictMode.VmPolicy.Builder()  
  10.               .detectLeakedSqlLiteObjects() //探測SQLite資料庫操作  
  11.               .penaltyLog()  //列印logcat  
  12.               .penaltyDeath()  
  13.               .build());  
  14.   }  
  15.   super.onCreate();  

上述代碼可以在Application的OnCreate中添加,這樣就能在程式啟動的最初一刻進行監控了。

輸出log如下:

 
  1. 02-27 10:03:56.122: DEBUG/StrictMode(16210): StrictMode policy violation; ~duration=696 ms:   
  2.                                                     android.os.StrictMode$StrictModeDiskReadViolation: policy=23 violation=2 
  3. 02-27 10:03:56.122: DEBUG/StrictMode(16210):     at android.os.StrictMode$AndroidBlockGuardPolicy.onReadFromDisk(StrictMode.java:745)  
  4. 02-27 10:03:56.122: DEBUG/StrictMode(16210):     at dalvik.system.BlockGuard$WrappedFileSystem.open(BlockGuard.java:228)  
  5. 02-27 10:03:56.122: DEBUG/StrictMode(16210):     at java.io.FileOutputStream.<init>(FileOutputStream.java:94)  
  6. 02-27 10:03:56.122: DEBUG/StrictMode(16210):     at java.io.FileOutputStream.<init>(FileOutputStream.java:66)  
  7. 02-27 10:03:56.122: DEBUG/StrictMode(16210):     at java.io.FileWriter.<init>(FileWriter.java:42)  
  8. 02-27 10:03:56.122: DEBUG/StrictMode(16210):     at org.zelos.asm.main.writeFile(main.java:30)  
  9. 02-27 10:03:56.122: DEBUG/StrictMode(16210):     at org.zelos.asm.main.onCreate(main.java:19)  
  10. 02-27 10:03:56.122: DEBUG/StrictMode(16210):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)  
  11. 02-27 10:03:56.122: DEBUG/StrictMode(16210):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1611)  
  12. 02-27 10:03:56.122: DEBUG/StrictMode(16210):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1663)  
  13. 02-27 10:03:56.122: DEBUG/StrictMode(16210):     at android.app.ActivityThread.access$1500(ActivityThread.java:117)  
  14. 02-27 10:03:56.122: DEBUG/StrictMode(16210):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:931)  
  15. 02-27 10:03:56.122: DEBUG/StrictMode(16210):     at android.os.Handler.dispatchMessage(Handler.java:99)  
  16. 02-27 10:03:56.122: DEBUG/StrictMode(16210):     at android.os.Looper.loop(Looper.java:123)  
  17. 02-27 10:03:56.122: DEBUG/StrictMode(16210):     at android.app.ActivityThread.main(ActivityThread.java:3683)  
  18. 02-27 10:03:56.122: DEBUG/StrictMode(16210):     at java.lang.reflect.Method.invokeNative(Native Method)  
  19. 02-27 10:03:56.122: DEBUG/StrictMode(16210):     at java.lang.reflect.Method.invoke(Method.java:507)  
  20. 02-27 10:03:56.122: DEBUG/StrictMode(16210):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)  
  21. 02-27 10:03:56.122: DEBUG/StrictMode(16210):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)  
  22. 02-27 10:03:56.122: DEBUG/StrictMode(16210):     at dalvik.system.NativeStart.main(Native Method)  
  23. 02-27 10:03:56.162: DEBUG/StrictMode(16210): StrictMode policy violation; ~duration=619 ms:   
  24.                                                     android.os.StrictMode$StrictModeDiskWriteViolation: policy=23 violation=1 
  25. 02-27 10:03:56.162: DEBUG/StrictMode(16210):     at android.os.StrictMode$AndroidBlockGuardPolicy.onWriteToDisk(StrictMode.java:732)  
  26. 02-27 10:03:56.162: DEBUG/StrictMode(16210):     at dalvik.system.BlockGuard$WrappedFileSystem.open(BlockGuard.java:230)  
  27. 02-27 10:03:56.162: DEBUG/StrictMode(16210):     at java.io.FileOutputStream.<init>(FileOutputStream.java:94)  
  28. 02-27 10:03:56.162: DEBUG/StrictMode(16210):     at java.io.FileOutputStream.<init>(FileOutputStream.java:66)  
  29. 02-27 10:03:56.162: DEBUG/StrictMode(16210):     at java.io.FileWriter.<init>(FileWriter.java:42)  
  30. 02-27 10:03:56.162: DEBUG/StrictMode(16210):     at org.zelos.asm.main.writeFile(main.java:30)  
  31. 02-27 10:03:56.162: DEBUG/StrictMode(16210):     at org.zelos.asm.main.onCreate(main.java:19)  
  32. 02-27 10:03:56.162: DEBUG/StrictMode(16210):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)  
  33. 02-27 10:03:56.162: DEBUG/StrictMode(16210):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1611)  
  34. 02-27 10:03:56.162: DEBUG/StrictMode(16210):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1663)  
  35. 02-27 10:03:56.162: DEBUG/StrictMode(16210):     at android.app.ActivityThread.access$1500(ActivityThread.java:117)  
  36. 02-27 10:03:56.162: DEBUG/StrictMode(16210):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:931)  
  37. 02-27 10:03:56.162: DEBUG/StrictMode(16210):     at android.os.Handler.dispatchMessage(Handler.java:99)  
  38. 02-27 10:03:56.162: DEBUG/StrictMode(16210):     at android.os.Looper.loop(Looper.java:123)  
  39. 02-27 10:03:56.162: DEBUG/StrictMode(16210):     at android.app.ActivityThread.main(ActivityThread.java:3683)  
  40. 02-27 10:03:56.162: DEBUG/StrictMode(16210):     at java.lang.reflect.Method.invokeNative(Native Method)  
  41. 02-27 10:03:56.162: DEBUG/StrictMode(16210):     at java.lang.reflect.Method.invoke(Method.java:507)  
  42. 02-27 10:03:56.162: DEBUG/StrictMode(16210):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)  
  43. 02-27 10:03:56.162: DEBUG/StrictMode(16210):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)  
  44. 02-27 10:03:56.162: DEBUG/StrictMode(16210):     at dalvik.system.NativeStart.main(Native Method) 

小結:Android 2.3關於StrictMode使用教程的內容介紹完了,希望通過Android 2.3內容的學習能對你有所協助。更多想要瞭解關於Android 2.3更多的內容,請參考編輯精選。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.