get Cpu Clock Speed - Android Hardware

Android examples for Hardware:CPU Frequency

Description

get Cpu Clock Speed

Demo Code

/*******************************************************************************
 * Copyright (c) 2011 MadRobot./*ww w . j a  v  a  2  s .co m*/
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the GNU Lesser Public License v2.1
 * which accompanies this distribution, and is available at
 * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
 * 
 * Contributors:
 *  Elton Kent - initial API and implementation
 ******************************************************************************/
//package com.java2s;
import java.io.BufferedReader;

import java.io.InputStreamReader;

public class Main {
    public static float getCpuClockSpeed() {
        float cpuclock = 0;
        try {
            final StringBuffer s = new StringBuffer();
            final Process p = Runtime.getRuntime()
                    .exec("cat /proc/cpuinfo");
            final BufferedReader input = new BufferedReader(
                    new InputStreamReader(p.getInputStream()));
            String line = "";
            while ((line = input.readLine()) != null
                    && s.toString().length() == 0) {
                if (line.startsWith("BogoMIPS")) {
                    s.append(line + "\n");
                }
            }
            final String cpuclockstr = s.substring(s.indexOf(":") + 2,
                    s.length());
            cpuclock = Float.parseFloat(cpuclockstr);
        } catch (final Exception err) {
            // if ANYTHING goes wrong, just report 0 since this is only used for
            // performance appraisal.
        }
        return cpuclock;
    }
}

Related Tutorials