本文介绍了按持续时间过滤Android StrictMode违规的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以根据持续时间过滤StrictMode违规?

Is there a way to filter StrictMode violations based on duration?

拥有这些东西有点烦人

StrictMode policy violation; ~duration=6 ms: android.os.StrictMode$StrictModeDiskWriteViolation: policy=31 violation=1

中毒我的logcat.

poisoning my logcat.

我认为StrictMode是一个有用的功能,但我只想在第一个开发阶段处理持续时间大于50毫秒的违规行为.

I think StrictMode is a useful feature, but I'd like to deal only with violations with a duration greater than, let's say, 50 ms, in the first development phase.

推荐答案

StrictMode API不支持按持续时间过滤.

The StrictMode API does not support filtering by duration.

但是您可以通过过滤StrictMode的日志报告轻松地做到这一点:

But you can easily do so by filtering StrictMode's log reports:


A.配置StrictMode以将错误写入日志:

public void onCreate() {
     if (DEVELOPER_MODE) {
         StrictMode.setThreadPolicy(new StrictMode.ThreadPolicy.Builder()
                 .detectDiskReads()
                 .detectDiskWrites()
                 .detectNetwork()
                 .penaltyLog() //<---------- write reports to log
                 .build());
     }
     super.onCreate();
}


B.阅读logcat行:

logcat = Runtime.getRuntime().exec(new String[]{"logcat", "-d"});
br = new BufferedReader(new InputStreamReader(logcat.getInputStream()),4*1024);
String line;
  final StringBuilder log = new StringBuilder();
  String separator = System.getProperty("line.separator");
    while ((line = br.readLine()) != null) {
          filterLogLine(line)
    }
}


C.按持续时间过滤行:

void filterLogLine(String line) {
    use StringTokenizer to parse the line
    get value of "~duration"
    and filter if lesser than your threshold
}

我让您找出行解析的确切细节.

I leave you to figure out the exact details of the line parsing.

这篇关于按持续时间过滤Android StrictMode违规的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

06-03 08:04