Copyright statement: Merge previous blog articles. Original works are not reprinted! Otherwise, legal liability will be held.
Sensormanager class: used to create an instance of the sensor service. This class provides many
This method is used to access and enumerate sensors and register and deregister sensor listeners. It also provides
Constants related to sensor accuracy, scan frequency, and calibration.
Sensor class: provides methods for obtaining sensor technical parameters. Such as version, type,
Manufacturer.
1. type_accelerometer: acceleration sensor (hardware sensor)
2. type_ambient_temperature: temperature sensor (hardware sensor)
3. type_gravity: gravity sensor (hardware or software sensor)
4. type_gyroscope: gyroscope sensor (hardware sensor)
5. type_light: light sensor (hardware sensor)
6. type_linear_acceleration: Linear Acceleration Sensor (hardware or software sensor)
7. type_magnetic_field: Magnetic Field Sensor (hardware sensor)
8. type_orientation: Direction sensor (Software Sensor), data from gravity and magnetic field sensors
9. type_pressure: Pressure Sensor (hardware sensor)
10. type_proximity: proximity sensor (hardware sensor)
11. type_relative_humidity: humidity sensor (hardware sensor)
12. type_rotation_vector: rotating vector sensor (hardware or software sensor)
Sensorevent class: this class is used by the system to create sensor event objects. This object can be provided
Sensor event-related information. The sensor event object contains the original information returned by the sensor.
Data, sensor type, data precision, and event triggering time.
Sensoreventlistener interface: this interface contains two callback methods.
When the value or precision changes, the system will call these two callback methods.
<Span style = "font-size: 12px;"> // obtain the sensor list sensormanager msensormanager = (sensormanager) getsystemservice (context. sensor_service); List <Sensor> msensors = msensormanager. getsensorlist (sensor. type_all); For (sensor: msensors) {msensortv. append (sensor. getname () + "\ n") ;}</span>
<Span style = "font-size: 12px;">/*** @ author linwb */public class mainactivity extends activity implements sensoreventlistener {private textview msensortv; private sensormanager msensormanager; private float [] adjustargs = new float [3]; @ override protected void oncreate (bundle savedinstancestate) {super. oncreate (savedinstancestate); setcontentview (R. layout. activity_main); msensortv = (textview) findviewbyid (R. id. sensortv); msensormanager = (sensormanager) getsystemservice (context. sensor_service) ;}@ override protected void onpause () {super. onpause (); msensormanager. unregisterlistener (this) ;}@ override protected void onresume () {super. onresume (); // The third parameter is the refresh frequency // sensormanager. sensor_delay_fastest fastest // sensormanager. sensor_delay_game // sensormanager. sensor_delay_normal normal // sensormanager. sensor_delay_ui User Interface msensormanager. registerlistener (this, msensormanager. getdefasensensor (sensor. type_accelerometer), sensormanager. sensor_delay_ui); msensormanager. registerlistener (this, msensormanager. getdefasensensor (sensor. type_gravity), sensormanager. sensor_delay_ui) ;}@ override public void onsensorchanged (sensorevent event) {Switch (event. sensor. getType () {Case sensor. type_accelerometer: // before removing impurities, ensure that the gravity sensor exists. // float x = adjustargs [0] * 0.9f + event. values [0] * (1-0.9f); // float y = adjustargs [1] * 0.9f + event. values [1] * (1-0.9f); // float z = adjustargs [2] * 0.9f + event. values [2] * (1-0.9f); string reslut = (event. values [0]) + "" + (event. values [1]) + "" + (event. values [2]-9.81f); msensortv. settext (reslut); break; Case sensor. type_gravity: adjustargs [0] = event. values [0]; adjustargs [1] = event. values [1]; adjustargs [2] = event. values [2]; break; default: break; }}@ override public void onaccuracychanged (sensor, int accuracy) {}</span>