How to find CPU load of any Android device programmatically

后端 未结 4 1271
南方客
南方客 2020-12-05 20:51

I want to have the same details in my android app. Anybody having any solution?

4条回答
  •  隐瞒了意图╮
    2020-12-05 21:40

    Since Android is based on a modified version of the Linux kernel, the same Linux command we can use to retrieve the CPU information according to the documentation.

    This virtual file identifies the type of processor used by your system - /proc/cpuinfo

    The command can be executed through ADB shell

    ADB Command:

    adb shell cat /proc/cpuinfo
    

    Using ProcessBuilder in Java API, you can execute the shell command from the Android application as below.

    Java API:

    try {
    
        String[] DATA = {"/system/bin/cat", "/proc/cpuinfo"};
        ProcessBuilder processBuilder = new ProcessBuilder(DATA);
        Process process = processBuilder.start();
        InputStream inputStream = process.getInputStream();
        byte[] byteArry = new byte[1024];
        String output = "";
        while (inputStream.read(byteArry) != -1) {
            output = output + new String(byteArry);
        }
        inputStream.close();
    
        Log.d("CPU_INFO", output);
    
    } catch (Exception ex) {
        ex.printStackTrace();
    }
    

    Sample output:

提交回复
热议问题