以编程方式读取应用程序的 logcat
Read logcat programmatically for an application
我已经阅读 this article。但它显示的大部分文本似乎都不在我的应用程序中。我如何过滤消息并让它只显示我的应用程序的日志。换句话说,我想像 Android Studio 中那样显示它(只显示来自我的应用程序的错误日志,显示时间戳等):
我尝试了类似 "logcat -d -v time" 的方法,但没有用。任何的想法?谢谢
尝试使用以下代码仅获取您的应用程序的日志。
public final class MyAppLogReader {
private static final String TAG = MyAppLogReader.class.getCanonicalName();
private static final String processId = Integer.toString(android.os.Process
.myPid());
public static StringBuilder getLog() {
StringBuilder builder = new StringBuilder();
try {
String[] command = new String[] { "logcat", "-d", "-v", "threadtime" };
Process process = Runtime.getRuntime().exec(command);
BufferedReader bufferedReader = new BufferedReader(
new InputStreamReader(process.getInputStream()));
String line;
while ((line = bufferedReader.readLine()) != null) {
if (line.contains(processId)) {
builder.append(line);
//Code here
}
}
} catch (IOException ex) {
Log.e(TAG, "getLog failed", ex);
}
return builder;
}
}
编辑
将以下权限添加到您的 AndroidManifest
文件
<uses-permission android:name="android.permission.READ_LOGS" />
我已经阅读 this article。但它显示的大部分文本似乎都不在我的应用程序中。我如何过滤消息并让它只显示我的应用程序的日志。换句话说,我想像 Android Studio 中那样显示它(只显示来自我的应用程序的错误日志,显示时间戳等):
我尝试了类似 "logcat -d -v time" 的方法,但没有用。任何的想法?谢谢
尝试使用以下代码仅获取您的应用程序的日志。
public final class MyAppLogReader {
private static final String TAG = MyAppLogReader.class.getCanonicalName();
private static final String processId = Integer.toString(android.os.Process
.myPid());
public static StringBuilder getLog() {
StringBuilder builder = new StringBuilder();
try {
String[] command = new String[] { "logcat", "-d", "-v", "threadtime" };
Process process = Runtime.getRuntime().exec(command);
BufferedReader bufferedReader = new BufferedReader(
new InputStreamReader(process.getInputStream()));
String line;
while ((line = bufferedReader.readLine()) != null) {
if (line.contains(processId)) {
builder.append(line);
//Code here
}
}
} catch (IOException ex) {
Log.e(TAG, "getLog failed", ex);
}
return builder;
}
}
编辑
将以下权限添加到您的 AndroidManifest
文件
<uses-permission android:name="android.permission.READ_LOGS" />