我想读取日志并将其保存到应用程序内的字符串中。我使用Log.d(..)保存日志。我读过您使用logcat吗?但这在运行android手机时不起作用。是否需要切换布尔值READ_LOG?

最佳答案

       public static void getLogCatDetails() {
                try {

                    Process process = Runtime.getRuntime().exec("logcat -d");

                    BufferedReader bufferedReader = new BufferedReader(
                            new InputStreamReader(process.getInputStream()));

                    StringBuilder log = new StringBuilder();

                    String line = "";

                    while ((line = bufferedReader.readLine()) != null) {
                        log.append(line);
                    }
}
catch (Exception e) {
        e.printStackTrace();
       }
        }

public static void clearLog()
    {
      try
      {
        Runtime.getRuntime().exec("logcat -c");
      }
      catch (Exception e) {
        e.printStackTrace();
       }
    }


您必须指定android.permission.READ_LOGS权限

关于java - 读取日志并将其保存到字符串,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/8326952/

10-12 01:31