Android Tutorial - Hardware : CPU
Get CPU information
package app.test; import java.io.File; import java.io.IOException; import java.io.InputStream; import java.text.DateFormat; import java.text.SimpleDateFormat; import java.util.Date; import java.util.Iterator; import java.util.List; import android.app.ActivityManager; import android.app.ActivityManager.RunningServiceInfo; import android.app.ActivityManager.RunningTaskInfo; import android.content.Context; import android.telephony.TelephonyManager; import android.util.DisplayMetrics; import android.util.Log; class CMDExecute { public synchronized String run(String[] cmd, String workdirectory) throws IOException { String result = ""; try { ProcessBuilder builder = new ProcessBuilder(cmd); // set working directory if (workdirectory != null) builder.directory(new File(workdirectory)); builder.redirectErrorStream(true); Process process = builder.start(); InputStream in = process.getInputStream(); byte[] re = new byte[1024]; while (in.read(re) != -1) { System.out.println(new String(re)); result = result + new String(re); } in.close(); } catch (Exception ex) { ex.printStackTrace(); } return result; } } public class Main { private static StringBuffer buffer; // cpu info public static String fetch_cpu_info() { String result = null; CMDExecute cmdexe = new CMDExecute(); try { String[] args = { "/system/bin/cat", "/proc/cpuinfo" }; result = cmdexe.run(args, "/system/bin/"); Log.i("result", "result=" + result); } catch (IOException ex) { ex.printStackTrace(); } return result; } }
Get CPU Bogo Mips
//package org.anddev.andengine.util; import java.io.IOException; import java.io.InputStream; import java.lang.reflect.Method; import java.util.Scanner; import java.util.regex.MatchResult; import android.content.Context; import android.content.pm.PackageInfo; import android.content.pm.PackageManager; import android.content.pm.PackageManager.NameNotFoundException; import android.os.Build; /** * (c) 2010 Nicolas Gramlich * (c) 2011 Zynga Inc. * * @author Nicolas Gramlich * @since 15:50:31 - 14.07.2010 */ class SystemUtils { private static final String BOGOMIPS_PATTERN = "BogoMIPS[\\s]*:[\\s]*(\\d+\\.\\d+)[\\s]*\n"; private static final String MEMTOTAL_PATTERN = "MemTotal[\\s]*:[\\s]*(\\d+)[\\s]*kB\n"; private static final String MEMFREE_PATTERN = "MemFree[\\s]*:[\\s]*(\\d+)[\\s]*kB\n"; public static float getCPUBogoMips() throws Exception { final MatchResult matchResult = SystemUtils.matchSystemFile("/proc/cpuinfo", BOGOMIPS_PATTERN, 1000); try { if(matchResult.groupCount() > 0) { return Float.parseFloat(matchResult.group(1)); } else { throw new Exception(); } } catch (final NumberFormatException e) { throw new Exception(e); } } private static MatchResult matchSystemFile(final String pSystemFile, final String pPattern, final int pHorizon) throws Exception { InputStream in = null; try { final Process process = new ProcessBuilder(new String[] { "/system/bin/cat", pSystemFile }).start(); in = process.getInputStream(); final Scanner scanner = new Scanner(in); final boolean matchFound = scanner.findWithinHorizon(pPattern, pHorizon) != null; if(matchFound) { return scanner.match(); } else { throw new Exception(); } } catch (final IOException e) { throw new Exception(e); } } }
Get CPU Frequency Current
//package org.anddev.andengine.util; import java.io.IOException; import java.io.InputStream; import java.lang.reflect.Method; import java.util.Scanner; import java.util.regex.MatchResult; import android.content.Context; import android.content.pm.PackageInfo; import android.content.pm.PackageManager; import android.content.pm.PackageManager.NameNotFoundException; import android.os.Build; /** * (c) 2010 Nicolas Gramlich * (c) 2011 Zynga Inc. * * @author Nicolas Gramlich * @since 15:50:31 - 14.07.2010 */ class SystemUtils { private static final String BOGOMIPS_PATTERN = "BogoMIPS[\\s]*:[\\s]*(\\d+\\.\\d+)[\\s]*\n"; private static final String MEMTOTAL_PATTERN = "MemTotal[\\s]*:[\\s]*(\\d+)[\\s]*kB\n"; private static final String MEMFREE_PATTERN = "MemFree[\\s]*:[\\s]*(\\d+)[\\s]*kB\n"; /** * @return in kiloHertz. * @throws SystemUtilsException */ public static int getCPUFrequencyCurrent() throws Exception { return SystemUtils.readSystemFileAsInt("/sys/devices/system/cpu/cpu0/cpufreq/scaling_cur_freq"); } private static int readSystemFileAsInt(final String pSystemFile) throws Exception { InputStream in = null; try { final Process process = new ProcessBuilder(new String[] { "/system/bin/cat", pSystemFile }).start(); in = process.getInputStream(); final String content = readFully(in); return Integer.parseInt(content); } catch (final Exception e) { throw new Exception(e); } } public static final String readFully(final InputStream pInputStream) throws IOException { final StringBuilder sb = new StringBuilder(); final Scanner sc = new Scanner(pInputStream); while(sc.hasNextLine()) { sb.append(sc.nextLine()); } return sb.toString(); } private static MatchResult matchSystemFile(final String pSystemFile, final String pPattern, final int pHorizon) throws Exception { InputStream in = null; try { final Process process = new ProcessBuilder(new String[] { "/system/bin/cat", pSystemFile }).start(); in = process.getInputStream(); final Scanner scanner = new Scanner(in); final boolean matchFound = scanner.findWithinHorizon(pPattern, pHorizon) != null; if(matchFound) { return scanner.match(); } else { throw new Exception(); } } catch (final IOException e) { throw new Exception(e); } } }
Get CPU Frequency Min
//package org.anddev.andengine.util; import java.io.IOException; import java.io.InputStream; import java.lang.reflect.Method; import java.util.Scanner; import java.util.regex.MatchResult; import android.content.Context; import android.content.pm.PackageInfo; import android.content.pm.PackageManager; import android.content.pm.PackageManager.NameNotFoundException; import android.os.Build; /** * (c) 2010 Nicolas Gramlich * (c) 2011 Zynga Inc. * * @author Nicolas Gramlich * @since 15:50:31 - 14.07.2010 */ class SystemUtils { private static final String BOGOMIPS_PATTERN = "BogoMIPS[\\s]*:[\\s]*(\\d+\\.\\d+)[\\s]*\n"; private static final String MEMTOTAL_PATTERN = "MemTotal[\\s]*:[\\s]*(\\d+)[\\s]*kB\n"; private static final String MEMFREE_PATTERN = "MemFree[\\s]*:[\\s]*(\\d+)[\\s]*kB\n"; /** * @return in kiloHertz. * @throws SystemUtilsException */ public static int getCPUFrequencyMin() throws Exception { return SystemUtils.readSystemFileAsInt("/sys/devices/system/cpu/cpu0/cpufreq/cpuinfo_min_freq"); } private static int readSystemFileAsInt(final String pSystemFile) throws Exception { InputStream in = null; try { final Process process = new ProcessBuilder(new String[] { "/system/bin/cat", pSystemFile }).start(); in = process.getInputStream(); final String content = readFully(in); return Integer.parseInt(content); } catch (final Exception e) { throw new Exception(e); } } public static final String readFully(final InputStream pInputStream) throws IOException { final StringBuilder sb = new StringBuilder(); final Scanner sc = new Scanner(pInputStream); while(sc.hasNextLine()) { sb.append(sc.nextLine()); } return sb.toString(); } private static MatchResult matchSystemFile(final String pSystemFile, final String pPattern, final int pHorizon) throws Exception { InputStream in = null; try { final Process process = new ProcessBuilder(new String[] { "/system/bin/cat", pSystemFile }).start(); in = process.getInputStream(); final Scanner scanner = new Scanner(in); final boolean matchFound = scanner.findWithinHorizon(pPattern, pHorizon) != null; if(matchFound) { return scanner.match(); } else { throw new Exception(); } } catch (final IOException e) { throw new Exception(e); } } }
Get CPU Frequency Max
//package org.anddev.andengine.util; import java.io.IOException; import java.io.InputStream; import java.lang.reflect.Method; import java.util.Scanner; import java.util.regex.MatchResult; import android.content.Context; import android.content.pm.PackageInfo; import android.content.pm.PackageManager; import android.content.pm.PackageManager.NameNotFoundException; import android.os.Build; /** * (c) 2010 Nicolas Gramlich * (c) 2011 Zynga Inc. * * @author Nicolas Gramlich * @since 15:50:31 - 14.07.2010 */ class SystemUtils { private static final String BOGOMIPS_PATTERN = "BogoMIPS[\\s]*:[\\s]*(\\d+\\.\\d+)[\\s]*\n"; private static final String MEMTOTAL_PATTERN = "MemTotal[\\s]*:[\\s]*(\\d+)[\\s]*kB\n"; private static final String MEMFREE_PATTERN = "MemFree[\\s]*:[\\s]*(\\d+)[\\s]*kB\n"; /** * @return in kiloHertz. * @throws SystemUtilsException */ public static int getCPUFrequencyMax() throws Exception { return SystemUtils.readSystemFileAsInt("/sys/devices/system/cpu/cpu0/cpufreq/cpuinfo_max_freq"); } private static int readSystemFileAsInt(final String pSystemFile) throws Exception { InputStream in = null; try { final Process process = new ProcessBuilder(new String[] { "/system/bin/cat", pSystemFile }).start(); in = process.getInputStream(); final String content = readFully(in); return Integer.parseInt(content); } catch (final Exception e) { throw new Exception(e); } } public static final String readFully(final InputStream pInputStream) throws IOException { final StringBuilder sb = new StringBuilder(); final Scanner sc = new Scanner(pInputStream); while(sc.hasNextLine()) { sb.append(sc.nextLine()); } return sb.toString(); } private static MatchResult matchSystemFile(final String pSystemFile, final String pPattern, final int pHorizon) throws Exception { InputStream in = null; try { final Process process = new ProcessBuilder(new String[] { "/system/bin/cat", pSystemFile }).start(); in = process.getInputStream(); final Scanner scanner = new Scanner(in); final boolean matchFound = scanner.findWithinHorizon(pPattern, pHorizon) != null; if(matchFound) { return scanner.match(); } else { throw new Exception(); } } catch (final IOException e) { throw new Exception(e); } } }
Get CPU Frequency Min Scaling
//package org.anddev.andengine.util; import java.io.IOException; import java.io.InputStream; import java.lang.reflect.Method; import java.util.Scanner; import java.util.regex.MatchResult; import android.content.Context; import android.content.pm.PackageInfo; import android.content.pm.PackageManager; import android.content.pm.PackageManager.NameNotFoundException; import android.os.Build; /** * (c) 2010 Nicolas Gramlich * (c) 2011 Zynga Inc. * * @author Nicolas Gramlich * @since 15:50:31 - 14.07.2010 */ class SystemUtils { private static final String BOGOMIPS_PATTERN = "BogoMIPS[\\s]*:[\\s]*(\\d+\\.\\d+)[\\s]*\n"; private static final String MEMTOTAL_PATTERN = "MemTotal[\\s]*:[\\s]*(\\d+)[\\s]*kB\n"; private static final String MEMFREE_PATTERN = "MemFree[\\s]*:[\\s]*(\\d+)[\\s]*kB\n"; /** * @return in kiloHertz. * @throws SystemUtilsException */ public static int getCPUFrequencyMinScaling() throws Exception { return SystemUtils.readSystemFileAsInt("/sys/devices/system/cpu/cpu0/cpufreq/scaling_min_freq"); } private static int readSystemFileAsInt(final String pSystemFile) throws Exception { InputStream in = null; try { final Process process = new ProcessBuilder(new String[] { "/system/bin/cat", pSystemFile }).start(); in = process.getInputStream(); final String content = readFully(in); return Integer.parseInt(content); } catch (final Exception e) { throw new Exception(e); } } public static final String readFully(final InputStream pInputStream) throws IOException { final StringBuilder sb = new StringBuilder(); final Scanner sc = new Scanner(pInputStream); while(sc.hasNextLine()) { sb.append(sc.nextLine()); } return sb.toString(); } private static MatchResult matchSystemFile(final String pSystemFile, final String pPattern, final int pHorizon) throws Exception { InputStream in = null; try { final Process process = new ProcessBuilder(new String[] { "/system/bin/cat", pSystemFile }).start(); in = process.getInputStream(); final Scanner scanner = new Scanner(in); final boolean matchFound = scanner.findWithinHorizon(pPattern, pHorizon) != null; if(matchFound) { return scanner.match(); } else { throw new Exception(); } } catch (final IOException e) { throw new Exception(e); } } }
Get CPU Frequency Max Scaling
//package org.anddev.andengine.util; import java.io.IOException; import java.io.InputStream; import java.lang.reflect.Method; import java.util.Scanner; import java.util.regex.MatchResult; import android.content.Context; import android.content.pm.PackageInfo; import android.content.pm.PackageManager; import android.content.pm.PackageManager.NameNotFoundException; import android.os.Build; /** * (c) 2010 Nicolas Gramlich * (c) 2011 Zynga Inc. * * @author Nicolas Gramlich * @since 15:50:31 - 14.07.2010 */ class SystemUtils { private static final String BOGOMIPS_PATTERN = "BogoMIPS[\\s]*:[\\s]*(\\d+\\.\\d+)[\\s]*\n"; private static final String MEMTOTAL_PATTERN = "MemTotal[\\s]*:[\\s]*(\\d+)[\\s]*kB\n"; private static final String MEMFREE_PATTERN = "MemFree[\\s]*:[\\s]*(\\d+)[\\s]*kB\n"; /** * @return in kiloHertz. * @throws SystemUtilsException */ public static int getCPUFrequencyMaxScaling() throws Exception { return SystemUtils.readSystemFileAsInt("/sys/devices/system/cpu/cpu0/cpufreq/scaling_max_freq"); } private static int readSystemFileAsInt(final String pSystemFile) throws Exception { InputStream in = null; try { final Process process = new ProcessBuilder(new String[] { "/system/bin/cat", pSystemFile }).start(); in = process.getInputStream(); final String content = readFully(in); return Integer.parseInt(content); } catch (final Exception e) { throw new Exception(e); } } public static final String readFully(final InputStream pInputStream) throws IOException { final StringBuilder sb = new StringBuilder(); final Scanner sc = new Scanner(pInputStream); while(sc.hasNextLine()) { sb.append(sc.nextLine()); } return sb.toString(); } private static MatchResult matchSystemFile(final String pSystemFile, final String pPattern, final int pHorizon) throws Exception { InputStream in = null; try { final Process process = new ProcessBuilder(new String[] { "/system/bin/cat", pSystemFile }).start(); in = process.getInputStream(); final Scanner scanner = new Scanner(in); final boolean matchFound = scanner.findWithinHorizon(pPattern, pHorizon) != null; if(matchFound) { return scanner.match(); } else { throw new Exception(); } } catch (final IOException e) { throw new Exception(e); } } }