Use of sensor Senso in Android
1.Sensor type
Android has a variety of sensors, the current Android SDK support sensors are: light sensor, rotation vector sensor, pressure sensor, gyroscope sensor, accelerometer, gravity sensor, direction sensor, magnetic field sensor, proximity sensor and so on. But not all mobile phones have all the sensors, the general high-end mobile phones with most sensors, after all, the sensor needs money, the price is naturally expensive.
2.Sensor Practical Application
So in Android development, how to use sensors to add sensor functionality to Android applications, such as shaking, by accelerating the sensor, triggering the search for nearby people, for example, each time you answer the phone, when the phone close to the ear, the hand of the black screen, this is the light sensor function.
3.Sensor Use steps
- Get the Sensor management object Sensormanager
- The class that creates the sensor event listener, which must implement the Android.hardware.SensorEventListner interface
- Registering the specified sensor using the Sensormanager.registerlistener method
4. Sensor Event Interface
Sensoreventlistner interface, the onsensorchanged () and onaccuracychanged () methods of this interface are used to handle the corresponding sensor events
1 Public Interface Sensoreventlistener {23 //Sensing data change call 4public void onsensorchanged (Sensorevent event); 5 When sensor accuracy changes, call 6public voidint accuracy); 7 }
5. Simple Code Implementation
Define Sensormanager
1 sensormanager sensormanager= (Sensormanager) Getsystemservice (Sensor_service);
Define an anonymous inner class or define a class to implement the Sensoreventlistner interface
1 PrivateSensoreventlistener sensoreventlistener=NewSensoreventlistener () {2 3 @Override4 Public voidonsensorchanged (Sensorevent event) {5 6 float[] values=event.values;7 floatx = values[0]; 8 floaty = values[1]; 9 floatz = values[2]; Ten } One @Override A Public voidOnaccuracychanged (sensor sensor,intaccuracy) { - - } the};
Registering and shutting down Sensor listener events
@Overrideprotected voidOnresume () {Super. Onresume (); if(sensormanager!=NULL) {Sensormanager.registerlistener (Sensoreventlistener, Sensormanager.getdefaultsensor (Sensor.TYPE_ACCELEROM Eter), sensormanager.sensor_delay_normal); }} @Overrideprotected voidOnPause () {Super. OnPause (); if(sensormanager!=NULL) {Sensormanager.unregisterlistener (Sensoreventlistener); } }
Use of sensor sensors in Android