How I get CPU usage and temperature information into an android app?

后端 未结 3 947
既然无缘
既然无缘 2020-12-18 09:26

I\'m developing an application and I don\'t know how can I get CPU usage and temperature, example in a textview. I try with TYPE_AMBIENT_TEMPERATURE to get temp

3条回答
  •  青春惊慌失措
    2020-12-18 09:48

    As OWADVL answered, you can ready the temperature also as a system file, like this:

    int temperature = readSystemFileAsInt("sys/class/thermal/thermal_zone0/temp");
    

    note that the readSystemFileAsInt is not a system call. I found the implementation here

    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);
        }
    }
    
    private 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();
    }
    

    About the CPU usage (load) you can look at this question and already working solution by Souch at his gitHub here

提交回复
热议问题