SDK Version: M3
Writing logs into logcat from yuor application is quite easy, reading the logcat programmatically is just a bit more tricky.
Reading logs is usually used for bugreport purposes.
The following code reads logs and displays then in a TextView:
public class LogTest extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
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);
}
TextView tv = (TextView)findViewById(R.id.textView1);
tv.setText(log.toString());
} catch (IOException e) {
}
}
}
When I first fint this solution, I was afraid that it will require some extreme permission because it runs logcat like a console command.
Read more: Hello Android