Get CPU number - Android Hardware

Android examples for Hardware:CPU Information

Description

Get CPU number

Demo Code


//package com.java2s;
import android.util.Log;

import java.io.File;
import java.io.FileFilter;

import java.util.regex.Pattern;

public class Main {
    private static final String TAG = "CpuManager";

    /**/*from  www  .  jav a  2s  .  com*/
     * CPU number
     * 
     * @return
     */
    public static int getNumCores() {
        // Private Class to display only CPU devices in the directory listing
        class CpuFilter implements FileFilter {
            @Override
            public boolean accept(File pathname) {
                // Check if filename is "cpu", followed by a single digit number
                if (Pattern.matches("cpu[0-9]", pathname.getName())) {
                    return true;
                }
                return false;
            }
        }

        try {
            File dir = new File("/sys/devices/system/cpu/");
            File[] files = dir.listFiles(new CpuFilter());
            Log.d("cpu", "CPU Count: " + files.length);
            return files.length;
        } catch (Exception e) {
            Log.d(TAG, "CPU Count: Failed.");
            e.printStackTrace();
            return 1;
        }
    }
}

Related Tutorials