gpt4 book ai didi

android - 以编程方式读取应用程序的 logcat

转载 作者:塔克拉玛干 更新时间:2023-11-02 20:27:35 24 4
gpt4 key购买 nike

我读过 this article已经。但它显示的大部分文本似乎都不在我的应用程序中。我如何过滤消息并让它只显示我的应用程序的日志。换句话说,我想像在 Android Studio 中那样显示它(只显示来 self 的应用程序的错误日志,显示时间戳等):

我尝试了类似“logcat -d -v time”的方法,但没有用。任何想法?谢谢。

enter image description here

最佳答案

尝试使用以下代码仅获取您的应用程序的日志。

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" />

关于android - 以编程方式读取应用程序的 logcat,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27957300/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com