Android examples for Hardware:CPU Information
Get the CPU arch type: x32 or x64
//package com.java2s; import java.io.BufferedReader; import java.io.File; import java.io.FileInputStream; import java.io.InputStream; import java.io.InputStreamReader; import java.lang.reflect.Method; import java.util.Locale; import android.content.Context; import android.util.Log; public class Main { public static final String CPU_ARCHITECTURE_TYPE_32 = "32"; public static final String CPU_ARCHITECTURE_TYPE_64 = "64"; private static final int EI_CLASS = 4; private static final int ELFCLASS64 = 2; /** The system property key of CPU arch type */ private static final String CPU_ARCHITECTURE_KEY_64 = "ro.product.cpu.abilist64"; /** The system libc.so file path */ private static final String SYSTEM_LIB_C_PATH = "/system/lib/libc.so"; private static final String SYSTEM_LIB_C_PATH_64 = "/system/lib64/libc.so"; private static final String PROC_CPU_INFO_PATH = "/proc/cpuinfo"; private static boolean LOGENABLE = false; /**/*from w w w. j a va 2s .c o m*/ * Get the CPU arch type: x32 or x64 */ public static String getArchType(Context context) { if (getSystemProperty(CPU_ARCHITECTURE_KEY_64, "").length() > 0) { return CPU_ARCHITECTURE_TYPE_64; } else if (isCPUInfo64()) { return CPU_ARCHITECTURE_TYPE_64; } else if (isLibc64()) { return CPU_ARCHITECTURE_TYPE_64; } else { return CPU_ARCHITECTURE_TYPE_32; } } private static String getSystemProperty(String key, String defaultValue) { String value = defaultValue; try { Class<?> clazz = Class.forName("android.os.SystemProperties"); Method get = clazz.getMethod("get", String.class, String.class); value = (String) (get.invoke(clazz, key, "")); } catch (Exception e) { if (LOGENABLE) { Log.d("getSystemProperty", "key = " + key + ", error = " + e.getMessage()); } } if (LOGENABLE) { Log.d("getSystemProperty", key + " = " + value); } return value; } /** * Read the first line of "/proc/cpuinfo" file, and check if it is 64 bit. */ public static boolean isCPUInfo64() { File cpuInfo = new File(PROC_CPU_INFO_PATH); if (cpuInfo != null && cpuInfo.exists()) { InputStream inputStream = null; BufferedReader bufferedReader = null; try { inputStream = new FileInputStream(cpuInfo); bufferedReader = new BufferedReader(new InputStreamReader(inputStream), 512); String line = bufferedReader.readLine(); if (line != null && line.length() > 0 && line.toLowerCase(Locale.US).contains("arch64")) { return true; } } catch (Throwable t) { } finally { try { if (bufferedReader != null) { bufferedReader.close(); } } catch (Exception e) { e.printStackTrace(); } try { if (inputStream != null) { inputStream.close(); } } catch (Exception e) { e.printStackTrace(); } } } return false; } /** * Check if system libc.so is 32 bit or 64 bit */ private static boolean isLibc64() { File libcFile = new File(SYSTEM_LIB_C_PATH); if (libcFile != null && libcFile.exists()) { byte[] header = readELFHeadrIndentArray(libcFile); if (header != null && header[EI_CLASS] == ELFCLASS64) { return true; } } File libcFile64 = new File(SYSTEM_LIB_C_PATH_64); if (libcFile64 != null && libcFile64.exists()) { byte[] header = readELFHeadrIndentArray(libcFile64); if (header != null && header[EI_CLASS] == ELFCLASS64) { return true; } } return false; } private static byte[] readELFHeadrIndentArray(File libFile) { if (libFile != null && libFile.exists()) { FileInputStream inputStream = null; try { inputStream = new FileInputStream(libFile); if (inputStream != null) { byte[] tempBuffer = new byte[16]; int count = inputStream.read(tempBuffer, 0, 16); if (count == 16) { return tempBuffer; } else { } } } catch (Throwable t) { } finally { if (inputStream != null) { try { inputStream.close(); } catch (Exception e) { e.printStackTrace(); } } } } return null; } }