Android Drivers-Sensor

Source: Internet
Author: User

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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.