【问题标题】:Filter Android StrictMode violations by duration按持续时间过滤 Android StrictMode 违规
【发布时间】:2014-07-31 10:06:11
【问题描述】:

有没有办法根据持续时间过滤 StrictMode 违规行为?

有这些有点烦人

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

毒化我的 logcat。

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

【问题讨论】:

    标签: android android-strictmode


    【解决方案1】:

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

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


    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
    }
    

    我让你弄清楚行解析的确切细节。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-07-28
      • 1970-01-01
      • 1970-01-01
      • 2020-02-05
      • 2023-01-23
      • 1970-01-01
      相关资源
      最近更新 更多