开发者

How I start a process to run logcat on Android?

开发者 https://www.devze.com 2023-02-02 02:30 出处:网络
I want to read Android system level log file.So I use the following code: Process mLogcatProc = null; BufferedReader reader = null;

I want to read Android system level log file.So I use the following code:

        Process mLogcatProc = null;
    BufferedReader reader = null;
    try {
        mLogcatProc = Runtime.getRuntime().exec(
                new String[] { "logcat", "-d",
                        "AndroidRuntime:E [Your Log Tag Here]:V *:S" });

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

        String line;
        final StringBuilder log = new StringBuilder();
        String separator = System.getProperty("line.开发者_运维知识库separator");

        while ((line = reader.readLine()) != null) {
            log.append(line);
            log.append(separator);
        }

    }

    catch (IOException e) {}
    finally {
        if (reader != null)
            try {
                reader.close();
            } catch (IOException e) {}

    }

I also used in AndroidManifest.xml. But I can't read any line. The StringBuilder log is empty. And the method mLogcatProc.waitFor return 0.

So how can I read the log ?


You cannot do this from a normal (non-suid) android application.

This is because a process needs to be running as root (or from adb) to read the logcat file in Android. If you have a terminal emulator installed on your [rooted] phone, try running logcat – it denies you permission to the log buffer unless you su to root.

The reason your StringBuffer ends up being empty is because the process outputs the error message to stderr (and you're reading stdin via your BufferedReader).


The code in original post works if you add correct permission to your AndroidManifest.xml

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

PS: You have to replace the "[Your Log Tag Here]" in your code with your actual TAG of course ;-)

0

精彩评论

暂无评论...
验证码 换一张
取 消