Android无法从应用程序运行logcat

Mat*_*ttC 1 logging android logcat android-logcat

所以我正在编写一个分析器,需要能够在分析会话期间记录异常.我的计划是使用logcat转储到SD卡或内部存储器上的文件,然后在分析会话完成时,将文件压缩并将其发送到服务器.我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());
    }
}
Run Code Online (Sandbox Code Playgroud)

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

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

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

Unable to open log device '/dev/log/main': Permission denied
Run Code Online (Sandbox Code Playgroud)

我以为将清单权限添加到我的应用程序然后允许我这样做?

Mat*_*ttC 14

故事的寓意是:仔细检查您的清单文件中的权限是否实际上是READ_LOGS而不仅仅是READ_LOG.