Reputation: 355
Does anyone know how to get the android device system log programatically using Java? This would be something similar to what is available on lower panel on the Dalvik Debug Monitor.
Thanks in advance.
Upvotes: 4
Views: 4075
Reputation: 789
I started with Mathias Conradt's answer above. It didn't work for me, but after working with it for a long time, I found out what tweaks it needed to get it working right. This will work. It doesn't require root access, special permissions, or anything.
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;
}
Upvotes: 0
Reputation: 28705
Untested with 'adb shell logcat', but I've used this to get other things from via 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[]{};
}
}
Upvotes: 5