【发布时间】:2010-08-25 00:10:42
【问题描述】:
有谁知道如何使用 Java 以编程方式获取 android 设备系统日志?这类似于 Dalvik 调试监视器下方面板上可用的内容。
提前致谢。
【问题讨论】:
-
您希望从您的 android 应用程序内部还是从外部输出此访问日志?您使用的是模拟器还是设备?
有谁知道如何使用 Java 以编程方式获取 android 设备系统日志?这类似于 Dalvik 调试监视器下方面板上可用的内容。
提前致谢。
【问题讨论】:
未使用“adb shell logcat”测试,但我已使用它通过 adb 获取其他内容:
public static String[] getAdbLogCat() {
try {
Process p = Runtime.getRuntime().exec("/path/to/adb shell logcat");
InputStream is = p.getInputStream();
InputStreamReader isr = new InputStreamReader(is);
BufferedReader br = new BufferedReader(isr);
final StringBuffer output = new StringBuffer();
String line;
ArrayList<String> arrList = new ArrayList<String>();
while ((line = br.readLine()) != null) {
System.out.println(line);
}
return (String[])arrList.toArray(new String[0]);
} catch (IOException e) {
System.err.println(e);
e.printStackTrace();
return new String[]{};
}
}
【讨论】:
我从上面的 Mathias Conradt 的回答开始。它对我不起作用,但是在使用了很长时间之后,我发现需要进行哪些调整才能使其正常工作。这将起作用。它不需要 root 访问权限、特殊权限或任何东西。
private static String getAdbLogCat()
{
String log = "";
String str;
try
{
String myStringArray[]= {"logcat", "-d"};
Process process = Runtime.getRuntime().exec(myStringArray);
InputStream is = process.getInputStream();
InputStreamReader isr = new InputStreamReader(is);
BufferedReader br = new BufferedReader(isr);
str = br.readLine();
while (str != null)
{
log += str;
str = br.readLine();
}
}
catch (IOException e)
{
}
return log;
}
【讨论】: