Recipe Using the Temperature and Light Sensor

The temperature sensor is used to determine temperature of the phone for internal hardware calibration. The light sensor measures ambient light and is used to automatically adjust the brightness of the screen.

These sensors are not available on all phones, but if they exist, the developer can use them for alternative reasons.The code to read the values from these sensors is shown in Listing 7.6. It can be added to the activity in the previous recipe to see the result.

Listing 7.6 Example Code to Access the Temperature and Light Sensors private final SensorEventListener mTListener

= new SensorEventListener(){

^Override public void onAccuracyChanged(Sensor sensor, int accuracy) {} ^Override public void onSensorChanged(SensorEvent event) { Log.v("test Temperature",

"onSensorChanged:"+event.sensor.getName()); if(event.sensor.getType()==Sensor.TYPE_TEMPERATURE){ tv2.setText("Temperature:"+event.values[0]);

private final SensorEventListener mLListener

= new SensorEventListener(){

^Override public void onAccuracyChanged(Sensor sensor, int accuracy) {} ^Override public void onSensorChanged(SensorEvent event) { Log.v("test Light",

"onSensorChanged:"+event.sensor.getName()); if(event.sensor.getType()==Sensor.TYPE_LIGHT){ tv3.setText("Light:"+event.values[0]);

sensorManager.registerListener(mTListener, sensorManager

ā– getDefaultSensor(Sensor.TYPE_TEMPERATURE), SensorManager.SENSOR_DELAY_FASTEST); sensorManager.registerListener(mLListener, sensorManager ā€¢getDefaultSensor(Sensor.TYPE_LIGHT), SensorManager.SENSOR_DELAY_FASTEST);

Character Building Thought Power

Character Building Thought Power

Character-Building Thought Power by Ralph Waldo Trine. Ralph draws a distinct line between bad and good habits. In this book, every effort is made by the writer to explain what comprises good habits and why every one needs it early in life. It draws the conclusion that habits nurtured in early life concretize into impulses in future for the good or bad of the subject.

Get My Free Ebook


Post a comment