天天看点

android2.3新增API StrictMode介绍

Application Not Responding)窗口的出现。

启用StrictMode 

推荐的使用StrictMode的方式是,在开发阶段,打开它,在发布应用前,关闭它。 

例如:在你的应用中,onCreate():

public void
  
if
new StrictMode.ThreadPolicy.Builder() //构造StrictMode
  
//当发生磁盘读操作时输出
  
//当发生磁盘写操作时输出
  
//访问网络时输出,这里可以替换为detectAll() 就包括了磁盘读写和网络I/O
  
//以日志的方式输出
  
               .build());  
  
new
  
//探测SQLite数据库操作
  
//以日志的方式输出
  
               .penaltyDeath()  
  
               .build());  
  
   }  
  
super.onCreate();      

当触发策略中的操作时系统会打印出一条StrictMode日志,格式如下:

  1. 02-27 10:03:56.122: DEBUG/StrictMode(16210): StrictMode policy violation; ~duration=696 ms: android.os.StrictMode$StrictModeDiskReadViolation: policy=23 violation=2
  2. 02 02-27 10:03:56.122: DEBUG/StrictMode(16210):     at android.os.StrictMode$AndroidBlockGuardPolicy.onReadFromDisk(StrictMode.java:745)    

 另外说明两点:

1.在android2.3版本直接在ui线程中访问网络会报错