7
 public class MainActivity extends Activity implements SensorEventListener { private TextView temperaturelabel; private SensorManager sensormanager; private Sensor temperature; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); temperaturelabel = (TextView) findViewById(R.id.text); sensormanager = (SensorManager)getSystemService(SENSOR_SERVICE); temperature= sensormanager.getDefaultSensor(Sensor.TYPE_AMBIENT_TEMPERATURE); temperaturelabel.setText(""+temperature.getPower()); } protected void onResume() { super.onResume(); sensormanager.registerListener(this, temperature, SensorManager.SENSOR_DELAY_FASTEST); } protected void onPause() { super.onPause(); sensormanager.unregisterListener(this); } public void onAccuracyChanged(Sensor sensor, int accuracy) {} public void onSensorChanged(SensorEvent event) { if (event.sensor.getType() != Sensor.TYPE_AMBIENT_TEMPERATURE) return; temperaturelabel.setText(""+temperature.getPower()); } } 

I want to get temperature from the device.

I wrote this code and I have tried it on HTC One X, but it didn't work.

6
  • Can you post the output or error. Commented Aug 14, 2012 at 9:38
  • nothing done. no error. no notification. no result Commented Aug 14, 2012 at 9:39
  • Does the HTC One X even have a temperature sensor? Commented Aug 14, 2012 at 9:49
  • of course. I installed temperature program from googleplay. it works. Commented Aug 14, 2012 at 9:51
  • 1
    Most of these programs get the temperature from the internet by sending you current position to a server. Commented Aug 14, 2012 at 9:51

2 Answers 2

7

A far as specification of htc one x says, its looks like this device doesn't have temperature sensor.

Take a look in its official specifications.

Sign up to request clarification or add additional context in comments.

1 Comment

There is a app in the android market called "Andro Sensor" which will give you the info of available and un-available sensors for your device .Try it
2

At the moment, only few devices have temperature sensor (e.g, Samsung S4).

You should always test if sensor is available, i.e., {

temperatureSensor = sensormanager.getDefaultSensor(Sensor.TYPE_AMBIENT_TEMPERATURE) if (temperatureSensor != null) { /* register listener and do other magic... */ } 

What you can do on HTC, is to find some other thermistor in the system and read sysfs path, where its value is exposed, e.g., thermistor in battery or in pressure sensor, if there is any in this phone.

Drawback of this approach is that these are raw thermistor values - sometimes you have to know, how to convert them to degrees of celsius and what's worse, they are not compensated (as the temperature from API is) - so e.g, if the phone will run some computation and processor will heat up the phone, that value can be easily 10 degreess higher than Ambient temperature and thus not really useful...

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.