How can I use hardwarepropertiesmanager(android) in android api 30, android 11

684 Views Asked by At

Now I'm trying to run

HardwarePropertiesManager hardwarePropertiesManager = getApplicationContext().getSystemService(HardwarePropertiesManager.class);
        hardwarePropertiesManager.getDeviceTemperatures(HardwarePropertiesManager.DEVICE_TEMPERATURE_CPU, HardwarePropertiesManager.TEMPERATURE_CURRENT);

to check cpu usage and temperature..

but, the exception occurred, which is

java.lang.RuntimeException: Unable to start activity 
ComponentInfo{com.example.___/com.example.___.MainActivity}: 
java.lang.SecurityException: The caller is neither a device 
owner, nor holding the DEVICE_POWER permission, nor the current 
VrListener.

is there any method to check cpu temperature and cpu usage(memory, core)??

1

There are 1 best solutions below

0
On

As the documentation mentions HardwarePropertiesManager is available for applications which are device owners - https://developer.android.com/reference/android/os/HardwarePropertiesManager

You can try reading the data from file

sys/class/hwmon/hwmonX/temp1_input

There are some changes based on vendors. You can refer to https://stackoverflow.com/a/59306562 for comprehensive list.