Android Wear - 以编程方式连续读取 LogCat 输出的代码不起作用

问题描述 投票:0回答:1

大家好,我最近编写了一些代码,在由我的应用程序中的后台服务启动的后台线程中连续读取 LogCat 输出。我不认为我的代码有错误,但它不起作用。

在我的主题中,我有:

@Override
public void run() {
    Process process = null;
    String cmd = "logcat -v time | grep 'AudioFocus  requestAudioFocus()'";

    try {
        process = Runtime.getRuntime().exec("su");
        DataOutputStream os = new DataOutputStream(process.getOutputStream());
        os.writeBytes("logcat -c" + "\n");
        os.writeBytes(cmd + "\n");
        os.flush();
    } catch(Exception e) {
    }

    BufferedReader reader = new BufferedReader(new InputStreamReader(process.getInputStream()));

    String line;

    try {
        while ( (line = reader.readLine()) != null ) {
            Log.e(StaticVal.TAG, line);

            // code for write line to my own log file
        }
    } catch(Exception e) {
    }
}

我还添加了清单权限:

<uses-permission android:name="android.permission.READ_LOGS" />

但是,当我将设备插入 PC 并使用 Android Studio 进行测试时,

Log.e(StaticVal.TAG, line);
没有打印出结果,也没有任何内容写入我自己的日志文件中。

如有任何帮助,我们将不胜感激!

(通过adb终端在我的设备上直接运行

logcat -v time | grep 'AudioFocus  requestAudioFocus()'
得到预期结果)

android logcat
1个回答
0
投票

此时,WearOS 上未授予 READ_LOGS 权限。即使在标准操作系统上,您现在也必须运行 pm grant 命令,但它在 WearOS 上不起作用。

遗憾的是,我想在特定情况下从最终用户那里获取应用程序的日志,但这根本不可能。

© www.soinside.com 2019 - 2024. All rights reserved.