gpt4 book ai didi

Android 无法从应用程序运行 logcat

转载 作者:行者123 更新时间:2023-11-29 14:19:36 26 4
gpt4 key购买 nike

所以我正在编写一个分析器,它需要能够在分析 session 期间记录异常。我的计划是使用 logcat 转储到 SD 卡或内部存储上的文件,然后在分析 session 完成后,将文件压缩并将其发送到服务器。我在 list 中添加了 android.permission.READ_LOGS,我的代码如下:

public void startLoggingExceptions() {
String filename = null;
String directory = null;
String fullPath = null;
String externalStorageState = null;

// The directory will depend on if we have external storage available to us or not
try {
filename = String.valueOf(System.currentTimeMillis()) + ".log";
externalStorageState = Environment.getExternalStorageState();

if (externalStorageState.equals(Environment.MEDIA_MOUNTED)) {
if(android.os.Build.VERSION.SDK_INT <= 7) {
directory = Environment.getExternalStorageDirectory().getAbsolutePath();
} else {
directory = ProfilerService.this.getExternalFilesDir(null).getAbsolutePath();
}
} else {
directory = ProfilerService.this.getFilesDir().getAbsolutePath();
}

fullPath = directory + File.separator + filename;

Log.w("ProfilerService", fullPath);
Log.w("ProfilerService", "logcat -f " + fullPath + " *:E");

exceptionLogger = Runtime.getRuntime().exec("logcat -f " + fullPath + " *:E");
} catch (Exception e) {
Log.e("ProfilerService", e.getMessage());
}
}

exceptionLogger 是一个 Process对象,然后我在分析 session 完成时调用 exceptionLogger.destroy()

我看到的行为是文件是在我指定的位置创建的,但文件中从来没有任何日志记录输出。我在模拟器中使用开发工具应用程序强制未处理的异常显示在日志中,但我的 logcat 输出文件仍然为空。有什么想法吗?

编辑:所以当我进入 adb shell,然后 SU 到分配给我的应用程序的用户帐户时,我在运行 logcat 时看到以下内容:

Unable to open log device '/dev/log/main': Permission denied

我曾以为将 list 权限添加到我的应用程序后就可以执行此操作了吗?

最佳答案

这个故事的寓意是:仔细检查您在 list 文件中的权限是否确实是 READ_LOGS 而不仅仅是 READ_LOG。

关于Android 无法从应用程序运行 logcat,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6219345/

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