The use of the Android sensor is accelerometer, direction sensor, magnetic field, pressure, temperature, light sense. The special distance sensor is introduced as an example.
The first class needs implement Sensoreventlistener. The use of sensors is divided into the following steps:
1. Create a new Sensormanager to manage sensor services. The code is:
Sensormanager smg= (Sensormanager) Getsystemservice (Context.sensor_service);
2. Call Sensormanager's Registerlistener () in the activity's Onresume () method to register the sensor's Listener (Onresume () is called when the activity is resumed). The Registerlsitener () has three parameters, the listener for the Sensoreventlistener Monitor sensor event, the sensor object, and the rate for acquiring the sensor data. The rate has three kinds: sensor_delay_fasetst: The fastest, the delay is small, consumes electricity. Sensor_delay_game: Suitable for game frequency, commonly used. Sensor_delay_normal: Normal frequency. SENSOR_DELAY_UI: Suitable for normal user frequency.
3. In onsensorchanged (Sensorevent event), write how the sensor data changes are handled. This is the method that the Sensoreventlistener interface must implement when the sensor value change is the callback method.