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

发布于 2024-10-21 03:40:31 字数 39 浏览 4 评论 0原文

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

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

凡间太子 2024-10-28 03:40:31

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

System.getProperty("os.version");

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

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 :

System.getProperty("os.version");

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

音盲 2024-10-28 03:40:31

如果您想要 Android 中显示有关手机的完整内核版本,请解析以下文件:
/proc/version

下面是 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";
    }
}

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

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

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";
    }
}
橙幽之幻 2024-10-28 03:40:31

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


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

假设您编写Java代码(据我所知Android应用程序是用Java编写的),这可能会帮助您: http://www.java-tips.org/java-se-tips/java.lang/how-to-execute -a-command-from-code.html

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


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

热血少△年 2024-10-28 03:40:31

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

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();
    }
}

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();
    }
}
攒眉千度 2024-10-28 03:40:31

如果您的目标是 API 21+ (Android 5+),请使用 android.system.Os.uname。例如:Os.uname().release

If you are targeting API 21+ (Android 5+), use android.system.Os.uname. For example: Os.uname().release

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文