【问题标题】:how to execute "adb logcat -d time > pathoffile\log.txt &" programatically如何以编程方式执行“adb logcat -d time > path of file\log.txt &”
【发布时间】:2011-06-29 08:18:47
【问题描述】:

我想在我的 java 代码中执行“adb logcat -d time > pathofile\log.txt &”。

我想在我的设备中创建我的 log.txt。

我为此编写了这段代码。

 ArrayList<String> commandLine = new ArrayList<String>();
        commandLine.add("logcat");
        commandLine.add("-d");
        commandLine.add("time");
        commandLine.add(">");
        commandLine.add(getApplicationContext().getFilesDir()
        //      + "/log.txt");   //already created the file at specified location.
        //commandLine.add("&");
Process process = Runtime.getRuntime().exec(commandLine.toArray(new String[0]));

上面的代码没有抛出任何错误,但是我的文件(log.txt)没有用日志语句更新..

请帮助我如何做到这一点,pl。建议是否有任何替代方法可以做到这一点.. 谢谢。

【问题讨论】:

  • 您是如何创建文件的?可能是权限问题?
  • 我创建了这样的文件: File logf= new File(getApplicationContext().getFilesDir() + "/log.txt"); logf.createNewFile();我可以通过导航到该文件目录来查看 log.txt 文件。所以我认为这不是权限问题。如果它会抛出 IO 异常。

标签: android


【解决方案1】:

改用-f 选项:

adb logcat -d time -f /mnt/sdcard/log.txt

注意&gt;是shell的io重定向功能。与exec()函数配合使用时无效。

【讨论】:

  • 感谢 inazaruk,它现在可以工作了,我们还需要在清单文件中添加 READ_LOGS 权限。
猜你喜欢
  • 1970-01-01
  • 2016-03-24
  • 2018-03-14
  • 2015-03-14
  • 2020-02-11
  • 1970-01-01
  • 1970-01-01
  • 2017-10-03
  • 1970-01-01
相关资源
最近更新 更多