如何将CPU使用率和温度信息输入android应用程序?
我正在开发一个应用程序,我不知道如何获得CPU使用率和温度,例如在textview中。 我尝试用如何将CPU使用率和温度信息输入android应用程序?,android,Android,我正在开发一个应用程序,我不知道如何获得CPU使用率和温度,例如在textview中。 我尝试用输入环境温度来获取温度,但它不起作用。 日志上说“我没有传感器”,但通过使用play store中的其他应用程序,我可以获得温度和频率 如果我使用其他传感器,如TYPE\u陀螺仪或其他传感器,代码工作正常,因此我不明白TYPE\u环境温度是如何工作的 对不起,我的英语不好…请帮助我..了解您可以使用的CPU频率 public static int[] getCPUFrequencyCurrent()
输入环境温度来获取温度,但它不起作用。
日志上说“我没有传感器”,但通过使用play store中的其他应用程序,我可以获得温度和频率
如果我使用其他传感器,如TYPE\u陀螺仪
或其他传感器,代码工作正常,因此我不明白TYPE\u环境温度
是如何工作的
对不起,我的英语不好…请帮助我..了解您可以使用的CPU频率
public static int[] getCPUFrequencyCurrent() throws Exception {
int[] output = new int[getNumCores()];
for(int i=0;i<getNumCores();i++) {
output[i] = readSystemFileAsInt("/sys/devices/system/cpu/cpu"+String.valueOf(i)+"/cpufreq/scaling_cur_freq");
}
return output;
}
对于温度()
另外,您首先需要检查传感器是否存在……如果不存在,则无法执行任何操作。我猜有些应用程序是假的
PS2。您始终可以对应用程序进行反向工程,以查看它们如何显示温度;) 这是因为您的设备根本没有温度计。很多旧设备都没有。
Android文档称,设备“可能”内置了这些传感器,但并非“必须”安装
其他向您显示温度的应用程序都是通过自己的方法计算的(这些方法可能在一定程度上有所不同)
我目前正在制作自己的应用程序,自己测量CPU温度…正如OWADVL回答的那样,您也可以将温度作为系统文件准备好,如下所示:
int temperature = readSystemFileAsInt("sys/class/thermal/thermal_zone0/temp");
请注意,readSystemFileAsInt不是系统调用。我找到了实现方法
关于CPU使用率(负载),您可以通过Souch在他的gitHub上查看并已经在运行的解决方案
public class TempSensorActivity extends Activity, implements SensorEventListener {
private final SensorManager mSensorManager;
private final Sensor mTempSensor;
public TempSensorActivity() {
mSensorManager = (SensorManager)getSystemService(SENSOR_SERVICE);
mTempSensor = mSensorManager.getDefaultSensor(Sensor.TYPE_AMBIENT_TEMPERATURE);
}
protected void onResume() {
super.onResume();
mSensorManager.registerListener(this, mTempSensor, SensorManager.SENSOR_DELAY_NORMAL);
}
protected void onPause() {
super.onPause();
mSensorManager.unregisterListener(this);
}
public void onAccuracyChanged(Sensor sensor, int accuracy) {
}
public void onSensorChanged(SensorEvent event) {
}
int temperature = readSystemFileAsInt("sys/class/thermal/thermal_zone0/temp");
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();
}