How to get the version of the Linux kernel using Android?
How can I get the version of th开发者_StackOverflow社区e Linux kernel in an Android application?
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
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: (xxxxxx@xxxxx.constant) */
"\\((?:[^(]*\\([^)]*\\))?[^)]*\\)\\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";
}
}
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
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();
}
}
精彩评论