Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Temperature sensor on Android

 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.

like image 741
Furkan Avatar asked Aug 14 '12 09:08

Furkan


1 Answers

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.

like image 84
Jviaches Avatar answered Sep 28 '22 19:09

Jviaches