如何使用Android获取Linux内核的版本?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5234574/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-05 03:09:54  来源:igfitidea点击:

How to get the version of the Linux kernel using Android?

androidlinuxversionkernel

提问by chaitanya

How can I get the version of the Linux kernel in an Android application?

如何在 Android 应用程序中获取 Linux 内核的版本?

回答by chaitanya

Invoke uname -rand read its output from stdout. It shouldn't be too complicated. The output is just the version number.

从 调用uname -r并读取其输出stdout。应该不会太复杂。输出只是版本号。



Runtime.getRuntime().exec("uname -r");

Assuming you write Java code (as far as I know Android Apps are written in Java), this might help you: http://www.java-tips.org/java-se-tips/java.lang/how-to-execute-a-command-from-code.html

假设您编写 Java 代码(据我所知,Android 应用程序是用 Java 编写的),这可能对您有所帮助:http: //www.java-tips.org/java-se-tips/java.lang/how-to-执行命令来自代码.html

回答by havchr

Not 100% sure, but I think calling "uname -r" would require root access. There is anyways a less dirty way to do this, which is :

不是 100% 确定,但我认为调用“uname -r”需要 root 访问权限。无论如何,有一种不那么脏的方法可以做到这一点,即:

System.getProperty("os.version");

I found this information here : http://cb1991.blogspot.com/2011/03/android-code-to-get-kernel-version.html

我在这里找到了这个信息:http: //cb1991.blogspot.com/2011/03/android-code-to-get-kernel-version.html

回答by 3c71

If you want the full Kernel version has shown in Android about phone, this is the file to parse: /proc/version

如果你想要完整的内核版本在 Android 中显示关于手机,这是要解析的文件:/proc/version

Here is an extract of Android source code that retrieves the actual kernel version string:

这是检索实际内核版本字符串的 Android 源代码的摘录:

private String getFormattedKernelVersion() {
    String procVersionStr;

    try {
        procVersionStr = readLine(FILENAME_PROC_VERSION);

        final String PROC_VERSION_REGEX =
            "\w+\s+" + /* ignore: Linux */
            "\w+\s+" + /* ignore: version */
            "([^\s]+)\s+" + /* group 1: 2.6.22-omap1 */
            "\(([^\s@]+(?:@[^\s.]+)?)[^)]*\)\s+" + /* group 2: ([email protected]) */
            "\((?:[^(]*\([^)]*\))?[^)]*\)\s+" + /* ignore: (gcc ..) */
            "([^\s]+)\s+" + /* group 3: #26 */
            "(?:PREEMPT\s+)?" + /* ignore: PREEMPT (optional) */
            "(.+)"; /* group 4: date */

        Pattern p = Pattern.compile(PROC_VERSION_REGEX);
        Matcher m = p.matcher(procVersionStr);

        if (!m.matches()) {
            Log.e(LOG_TAG, "Regex did not match on /proc/version: " + procVersionStr);
            return "Unavailable";
        } else if (m.groupCount() < 4) {
            Log.e(LOG_TAG, "Regex match on /proc/version only returned " + m.groupCount()
                    + " groups");
            return "Unavailable";
        } else {
            return (new StringBuilder(m.group(1)).append("\n").append(
                    m.group(2)).append(" ").append(m.group(3)).append("\n")
                    .append(m.group(4))).toString();
        }
    } catch (IOException e) {
        Log.e(LOG_TAG,
            "IO Exception when getting kernel version for Device Info screen",
            e);

        return "Unavailable";
    }
}

回答by Abandoned Cart

If you are doing this from an app, this will give you the kernel version string:

如果您从应用程序执行此操作,这将为您提供内核版本字符串:

public static String readKernelVersion() {
    try {
        Process p = Runtime.getRuntime().exec("uname -a");
        InputStream is = null;
        if (p.waitFor() == 0) {
            is = p.getInputStream();
        } else {
            is = p.getErrorStream();
        }
        BufferedReader br = new BufferedReader(new InputStreamReader(is),
                BUFFER_SIZE);
        String line = br.readLine();
        br.close();
        return line;
    } catch (Exception ex) {
        return "ERROR: " + ex.getMessage();
    }
}