Proximity sensor support is very similar to the orientation sensor discussed earlier. I have a TextView on which to display results:
As with the orientation sensor, I’m implementing the SensorEventListener interface. The code of the onCreate method is practically the same as that of the orientation sensor, except that the listener type Sensor.TYPE_PROXIMITY and not the TYPE_ORIENTATION type is used as a parameter. As with the orientation sensor, the onSensorChanged method is automatically called when the sensor readings change. On line 33 I display on the textView component read. On the Samsung ACE3 only two values were displayed – 0 or 5 if I approached my hand less than 2cm.
In the very similar way we can use other sensors – eg temperature sensor (if equipped). If this is the case, replace Sensor.TYPE_PROXIMITY with Sensor.TYPE_TEMPERATURE in line 22.