1

我阅读了其他帖子,无法弄清楚“诀窍”。

我查看了 Log Collector,但无法使用单独的 APK。我基本上使用相同的方法,并且我一直没有得到任何关于流程输入流的信息。

我在清单中有 READ_LOGS。

在我的默认活动中,我可以获取日志,但如果我将逻辑移动到另一个活动或使用异步任务,则不会返回任何输出。

此代码来自我的默认活动...内联,我将其转储到日志中

 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);
    }
    Log.d(LOGTAG, "Logcat: " +log.toString());
 } catch (IOException e) {}

如果我将它包装在 asynctask 中或只是将它内联到另一个活动中,它不会返回任何内容

    ArrayList<String> commandLine = new ArrayList<String>();
    //terminate on completion and suppress everything except the filter
    commandLine.add("logcat -d -s");
       ...
    //replace asynctask with inline (could not get log in asynctask)
    showProgressDialog(getString(R.string.acquiring_log_progress_dialog_message));
    final StringBuilder log = new StringBuilder();
    BufferedReader bufferedReader = null;
    try{

        Process process = Runtime.getRuntime().exec(commandLine.toArray(new String[0]));
        bufferedReader = new BufferedReader(new InputStreamReader(process.getInputStream()));

        String line;
        while ((line = bufferedReader.readLine()) != null){ 
            log.append(line);
            log.append(MangoApp.LINE_SEPARATOR); 
        }


        sendIntent.putExtra(Intent.EXTRA_TEXT, log.toString());
        startActivity(Intent.createChooser(sendIntent, getString(R.string.chooser_title)));
        dismissProgressDialog();
        dismissMainDialog();
        finish();           
    } 
    catch (IOException e){
        dismissProgressDialog();
        showErrorDialog(getString(R.string.failed_to_get_log_message));
        Log.e(LOGTAG, "Log collection failed: ", e);//$NON-NLS-1$
    } finally {
        if (bufferedReader != null) {
            try {
                bufferedReader.close();
            } catch (IOException ignore) {}
        }
    }

任何人都可以发现差异或解释魔术吗?我很确定命令行在第二个版本中是正确的,所以我摸不着头脑。我在模拟器上使用 2.1 SDK 7。

谢谢

4

2 回答 2

6

希望这会有所帮助,您不必自己创建文件,只需执行以下命令即可获取错误信息。

Runtime.getRuntime().exec("logcat -v time -r 100 -f /sdcard/log.txt *:E");

Logcat 参数选项:

-r <size in kilobytes> -> for specifying the size of file  
-f <filename> -> file to which you want to write the logs.
于 2011-06-03T14:00:56.993 回答
2

你能在没有 ArrayList 的情况下试试吗?只需传递命令字符串,我已经通过以下方式实现了它(没有 ArrayList)。这个对我有用。

        String baseCommand = "logcat -v time";
        baseCommand += " MyApp:I "; // Info for my app
        baseCommand += " *:S "; // Silence others

        ServicesController.logReaderProcess = Runtime.getRuntime().exec(baseCommand);
于 2011-04-05T20:44:10.177 回答