is the Android system architecture diagram, from which you can tell that sensor must penetrate all levels of the architecture. According to the architecture hierarchy, the sensor architecture is analyzed in five ways:
1. The app layer of the sensor architecture;
2. The framework layer of the sensor architecture;
3. Libraries layer of sensor architecture;
4. The HAL layer of the sensor architecture;
5. Driver of the sensor architecture.
1. Sensor Architecture App Layer
Take G-sensor, for example, to write a simple apk to see how the sensor works in the app layer, which typically requires the following four steps to implement a sensor application.
STEP1: To get the sensor service through Getsystemservice is actually initializing a Sensormanager instance;
1 Sensormanager Msensormanager = (sensormanager) getsystemservice (Sensor_service);
STEP2: The sensor object of the specified type is obtained by means of the Sensormanager Getdefaultsensor method;
1 // Gravity sensor
STEP3: Implement the Onsensorchanged and onaccuracychanged method of Sensoreventlistener interface;
1SENSOREVENTLISTENERLSN =NewSensoreventlistener () {2 Public voidonsensorchanged (sensorevent e) {3 //This is done when the value of the sensor changes4 }5 6 Public voidOnaccuracychanged (Sensor S,intaccuracy) { 7 //when the accuracy of the sensor changes, this is done8 }9};
STEP4: Register monitoring by Sensormanager's Registerlistener method to get the sensor change value.
1 msensormanager.registerlistener (lsn,sensor, sensormanager.sensor_delay_game);
Note: The Onsensorchanged method is called when the value of the sensor changes, and the Onaccuracychanged method is called when the sensor's precision changes.
Complete Sample code:
1 Public classMainextendsActivity {2 Private floatx, y, Z; 3 protected voidonCreate (Bundle savedinstancestate) {4 Super. OnCreate (savedinstancestate); 5Sensormanager msensormanager=(Sensormanager) Getsystemservice (Sensor_service); 6SENSOR sensor =msensormanager.getdefaultsensor (sensor.type_gravity); 7Sensoreventlistener LSN =NewSensoreventlistener () {8 9 Public voidonsensorchanged (sensorevent e) {TenSystem.out.println (e.value[0]); OneSystem.out.println (e.value[1]); ASystem.out.println (e.value[2]); - } - Public voidOnaccuracychanged (Sensor S,intaccuracy) { the } - }; - Msensormanager.registerlistener (LSN, sensor, sensormanager.sensor_delay_game); -}
Android Drivers-Sensor