[Android Memory] Android 的 StrictMode

android的2.3 之后引入的StrictMode 对网络的访问做了限制啊。

public void onCreate() {
     if (DEVELOPER_MODE) {
         StrictMode.setThreadPolicy(new StrictMode.ThreadPolicy.Builder()
                 .detectDiskReads()
                 .detectDiskWrites()
                 .detectNetwork()   // or .detectAll() for all detectable problems
                 .penaltyLog()
                 .build());
         StrictMode.setVmPolicy(new StrictMode.VmPolicy.Builder()
                 .detectLeakedSqlLiteObjects()
                 .detectLeakedClosableObjects()
                 .penaltyLog()
                 .penaltyDeath()
                 .build());
     }
     super.onCreate();
 }  

    1.在2.3版本以后加入了StrictMode类,而在3.0在网络上能感觉到有更加严格的限制,更多的查询API上的StrictMode ;
      2.使用的时候只需要在你项目运行的入口Activity的OnCreate中放入这段代码,那么整个项目程序都有用。不需要每个Activity里面加入。

      3.StrictMode类一般是用来调试的,在程序运行中会打印很多消息,那是告诉你你的项目程序需要改进的地方。在Android项目中,最好的是让界面与后台装载程序分开来。总之,如果你的程序代码非常符合Android规范要求,那么你完全可以不使用上面的代码...

posted @ 2014-12-03 17:32  demoblog  阅读(303)  评论(0编辑  收藏  举报