How to get CPU Information in Android Programmatically in java?

You can get CPU Cores, supported ABIS, minimum and maximum CPU Frequency, BOGO MIPS, CPU clock speed, minimum and maximum scaling frequency and other

 In this tutorial, we are going to get and display the most important CPU Factors like a number of CPU Cores, get all the supported ABIS, processor name, model name, the minimum and maximum frequency of the CPU, BOGO MIPS, the clock speed of Android, to check whether the device is 32bits or 64bits, minimum and maximum scaling frequency, get GPU Renderer, GPU Vendor and extensions, GPU Version, Checking whether GPU is supported by Android or not, CPU parts, Model name, etc. This tutorial will be very helpful if you are developing applications like CPU-Z.

First, you have to add these methods to your Java Class :
/* Codes are from github.com/oseamiya/deviceinformation/.../CPUInformation */
private static int readSystemFileAsInt(final String systemFile){
        InputStream inputStream = null;
        try {
            final Process process = new ProcessBuilder(new String[] {"/system/bin/cat" , systemFile}).start();
            inputStream = process.getInputStream();
            final String content = readFully(inputStream);
            return Integer.parseInt(content);
        } catch (IOException e) {
            e.printStackTrace();
            return 0;
        }

    }

    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 systemFile, final String pattern, final int horizon) throws Exception {
        InputStream in = null;
        try {
            final Process process = new ProcessBuilder(new String[] { "/system/bin/cat", systemFile }).start();

            in = process.getInputStream();
            final Scanner scanner = new Scanner(in);

            final boolean matchFound = scanner.findWithinHorizon(pattern, horizon) != null;
            if(matchFound) {
                return scanner.match();
            } else {
                throw new Exception();
            }
        } catch (final IOException e) {
            throw new Exception(e);
        }

    }


1. Get the Number of CPU Cores in Android programmatically?
public int getNumberOfCores(){
        return Runtime.getRuntime().availableProcessors();
    }
2. Get all the supported ABIS in Android programmatically
public static String[] getSupportedABIs(){
        return Build.SUPPORTED_ABIS;
    }

3. Get the minimum CPU Frequency in Android programmatically

 public static int getMinimumFrequency(){
        return CpuInformation.readSystemFileAsInt("/sys/devices/system/cpu/cpu0/cpufreq/cpuinfo_min_freq");
    }

4. Get the maximum CPU Frequency in Android programmatically

public static int getMaximumFrequency(){
        return CpuInformation.readSystemFileAsInt("/sys/devices/system/cpu/cpu0/cpufreq/cpuinfo_max_freq");
    }

5. Get BOGO MIPs of CPU in Android programmatically 

public static float getBogoMips() throws Exception {
        final MatchResult matchResult = CpuInformation.matchSystemFile("/proc/cpuinfo", "BogoMIPS[\\s]*:[\\s]*(\\d+\\.\\d+)[\\s]*\n", 1000);

        try {
            if(matchResult.groupCount() > 0) {
                return Float.parseFloat(matchResult.group(1));
            } else {
                throw new Exception();
            }
        } catch (final NumberFormatException e) {
            throw new Exception(e);
        }
    }

6. Get clock speed of CPU in Android programmatically

public static int getClockSpeed(){
        return CpuInformation.readSystemFileAsInt("/sys/devices/system/cpu/cpu0/cpufreq/scaling_cur_freq");
    }

7. Check if CPU is either 64-Bits or 32-Bits in Android programmatically

public static boolean is64Bit(){
        return Build.SUPPORTED_64_BIT_ABIS.length > 0;
    }

8. Get minimum scaling frequency of Android programmatically 

 public static int getMinScalingFrequency(){
        return CpuInformation.readSystemFileAsInt("/sys/devices/system/cpu/cpu0/cpufreq/scaling_min_freq");
    }

9. Get maximum scaling frequency of Android programmatically

 public static int getMaxScalingFrequency(){
        return CpuInformation.readSystemFileAsInt("/sys/devices/system/cpu/cpu0/cpufreq/scaling_max_freq");
    }

10. Get all other CPU Information at once 

public String getCpuInformation(){
        ProcessBuilder processBuilder;
        Process process;
        String[] DATA = {"/system/bin/cat" , "/proc/cpuinfo"};
        InputStream inputStream;
        byte[] byteArray;
        StringBuilder Holder = new StringBuilder();

        processBuilder = new ProcessBuilder(DATA);
        byteArray = new byte[1024];
        try {
            process = processBuilder.start();
            inputStream = process.getInputStream();
            while (inputStream.read(byteArray) != -1){
                Holder.append(new String(byteArray));
            }
            inputStream.close();
            return Holder.toString();
        } catch (IOException e) {
            e.printStackTrace();
            return "Exception Occurred";
        }
    }

Post a Comment

Cookie Consent
We serve cookies on this site to analyze traffic, remember your preferences, and optimize your experience.
Oops!
It seems there is something wrong with your internet connection. Please connect to the internet and start browsing again.
AdBlock Detected!
We have detected that you are using adblocking plugin in your browser.
The revenue we earn by the advertisements is used to manage this website, we request you to whitelist our website in your adblocking plugin.
Site is Blocked
Sorry! This site is not available in your country.