how to get device 's temperature in android [closed]

坚强是说给别人听的谎言 提交于 2020-01-10 02:06:51

问题


I want to get temperature from device. but I don t know how I can do. please help me writing simple. thanks.


回答1:


You can use TYPE_AMBIENT_TEMPERATURE for battery or CPU temperature. TYPE_TEMPERATURE is the depcrecated constant.

The modified version of code available at documentation should 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 documentation for more.



来源:https://stackoverflow.com/questions/11931681/how-to-get-device-s-temperature-in-android

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!