我的Android应用程序发布了许多日志消息。我试图在同一个应用程序中显示所有这些日志消息。应用程序有一个UI元素(文本视图),它应列出我的应用程序记录的所有消息。如何访问按应用程序名称(例如按名称com.mycompany.myapp
)
答案 0 :(得分:3)
要在应用程序中显示系统日志,过滤您自己的应用程序名称,您可以将以下内容作为内部类插入:
private static final String LogCatCommand = "logcat ActivityManager:I *:S";
private static final String ClearLogCatCommand = "logcat -c";
private class MonitorLogThread extends Thread{
public MonitorLogThread(){
}
BufferedReader br;
@Override
public void run() {
try {
Process process;
process = Runtime.getRuntime().exec(ClearLogCatCommand);
process = Runtime.getRuntime().exec(LogCatCommand);
br = new BufferedReader(new InputStreamReader(process.getInputStream()));
String line;
// Check if it matches the pattern
while(((line=br.readLine()) != null) && !this.isInterrupted()){
// Filter for your app-line
if (line.contains("your-filter-string")){
Log.i("myAppTag", "Found log-entry for my app:" + line);
}
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
在你的onCreate-Method:
Thread mThread = new MonitorLogThread();
mThread.start();
您可能需要调整此示例才能使其正常工作!