如何在android中获取设备的温度
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11931681/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
how to get device 's temperature in android
提问by Furkan
I want to get temperature from device. but I don t know how I can do. please help me writing simple. thanks.
我想从设备获取温度。但我不知道我该怎么做。请帮我写简单。谢谢。
回答by prayagupd
You can use TYPE_AMBIENT_TEMPERATUREfor battery or CPU temperature. TYPE_TEMPERATUREis the depcrecated constant.
您可以将TYPE_AMBIENT_TEMPERATURE用于电池或 CPU 温度。TYPE_TEMPERATURE是已弃用的常量。
The modified version of code available at documentationshould be like this:
文档中可用的修改后的代码版本应该是这样的:
import android.app.Activity;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
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) {
}
}
Look at sensor documentationfor more.
查看传感器文档了解更多信息。