Reputation: 23
I want to read logcat from an app(and screen for a specific string in the logs). BUT everything I've tried so far causes the app to stop(button looks pressed, keyboard does nothing) any ideas? this is my code so far(copied it from the internet):
try {
Runtime.getRuntime().exec("su").waitFor();
Runtime.getRuntime().exec("logcat -c").waitFor();
Process process = Runtime.getRuntime().exec("logcat");
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(process.getInputStream()));
String line = "";
while ((line = bufferedReader.readLine()) != null) {
if(line.contains(s)){
Toast.makeText(MainActivity.this, "found S!", Toast.LENGTH_SHORT).show();
break;
}
}
}catch (IOException e) {}
Upvotes: 1
Views: 3449
Reputation: 2320
You can run logcat
and read the log in a service.
See logdog, I use it to read the logcat, it works fine.
Simple example:
/* put these code in onCreate() of a service */
Process process = Runtime.getRuntime().exec("su");
BufferedReader reader = new BufferedReader(new InputStreamReader(process.getInputStream()));
DataOutputStream dos = new DataOutputStream(process.getOutputStream());
dos.writeBytes("logcat -b events -v time\n");
dos.flush();
/* read log, put these code out of onCreate() in service and periodically call this code */
while (reader.ready()) {
line = reader.readLine();
//handle the log
}
Upvotes: 0
Reputation: 3210
Try this code:
log = (TextView)findViewById(R.id.log);
logContainer = (ScrollView)findViewById(R.id.logContainer);
new AsyncTask<Void, String, Void>() {
@Override
protected Void doInBackground(Void... params) {
try {
Process process = Runtime.getRuntime().exec("logcat");
BufferedReader bufferedReader = new BufferedReader(
new InputStreamReader(process.getInputStream()));
String line = "";
while ((line = bufferedReader.readLine()) != null) {
if (line.contains("<<<<<<<<YOUR TAG>>>>>>>>"))
publishProgress(line);
}
}
catch (IOException e) {
}
return null;
}
@Override
protected void onProgressUpdate(String... values) {
log.append(values[0] + "\n");
logContainer.post(new Runnable() {
@Override
public void run() {
logContainer.fullScroll(View.FOCUS_DOWN);
}
});
}
}.execute();
Upvotes: 3