如何在我的设备上获取logcat以显示来自所有进程的日志

Hou*_*ell 10 android logcat

我正在尝试编写一个能够读取设备上所有日志的应用程序.我有一个客户端/服务架构,我看到来自客户端和服务进程的日志消息,但我没有看到来自手机上任何其他应用程序的任何消息(我确实看到使用桌面logcat的其他消息).

我需要root吗?

代码片段

表现

<uses-permission android:name="android.permission.READ_LOGS" />
Run Code Online (Sandbox Code Playgroud)

日志阅读器

Runtime.getRuntime().exec("logcat -c").waitFor();

Process process = Runtime.getRuntime().exec("logcat -v long *:*");
BufferedReader reader = 
    new BufferedReader(new InputStreamReader(process.getInputStream()));
while (true) {
    String nextLine = reader.readLine();
    if (!nextLine.contains("LogWatcher-D")) {
        Log.w("LogWatcher-D", "See: " + nextLine);
    }

    // Process line
}
Run Code Online (Sandbox Code Playgroud)

Com*_*are 12

在Android 4.1+上,您只能访问由您的进程记录的日志消息,除非您拥有该READ_LOGS权限.该权限要求您的应用程序必须使用签署设备固件的相同签名密钥进行签名,或者您的应用程序安装在系统分区上.