这个类是Android2.3才出的,所以在2.3以前不可以用,这个类可以帮我们检查一些常见的语法、在非UI线程中操作UI、网络、读写磁盘等错误,总之用下它自我感觉还不错
用法是:
public void 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中添加,这样就能在程序启动的最初一刻进行监控了。