Filter Android StrictMode violations by duration

前端 未结 1 1081
深忆病人
深忆病人 2021-01-20 06:48

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

It\'s getting a bit annoying having these

StrictMode policy violation; ~duration=6          


        
相关标签:
1条回答
  • 2021-01-20 07:13

    The StrictMode API does not support filtering by duration.

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


    A. Configure StrictMode to write errors to log:

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


    B. Read logcat lines:

    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. filter lines by duration:

    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.

    0 讨论(0)
提交回复
热议问题