[Android Note 5] Android sensor introduction (I) acquisition of gravity sensor Acceleration

Source: Internet
Author: User

The instructor of the fetc project proposed a new requirement. He wanted to show the user's current movement direction in the game map. If he used GPS, it was obviously unreliable, so he thought of the powerful Android sensor...

Many mobile devices have built-in sensors. Android abstracts these sensors through the sensor and sensormanager classes, and these sensors can be used by Android devices.

1. Sensor class

The SDK only has one sentence to introduce "class representing a sensor. use getsensorlist (INT) to get the list of available sensors. ", indicates a sensor class. You can use the getsensorlist method (this method belongs to the sensormanager to be discussed later) to obtain all available sensors. This method returns a list <Sensor>

The following list shows all the services provided by sensor.
Certificate ----------------------------------------------------------------------------------------------------------------------------------------------------------
Constants
Int type_accelerometer a constant describing an Accelerometer Sensor Type. // The Three-Axis acceleration sensor returns the acceleration unit m/s2 of the three axes.
Int type_all a constant describing all sensor types. // used to list all sensors
Int type_gravity a constant describing a gravity sensor type. // gravity Sensor
Int type_gyroscope a constant describing a gyroscope sensor type // The gyroscope can determine the direction and return the angle on the three coordinate axes.
Int type_light a constant describing an light sensor type. // The light sensor unit is Lux.
Int type_linear_acceleration a constant describing a linear acceleration sensor type. // linear acceleration
Int type_magnetic_field a constant describing a magnetic field sensor type. // magnetic field sensing returns the values of the three coordinate axes micro tesla
Int type_orientation this constant is deprecated. Use sensormanager. getorientation () instead. // you can use this method to obtain if the direction sensor is outdated.
Int type_pressure a constant describing a pressure sensor type // pressure sensor unit: qianpascal
Int type_proximity a constant describing an proximity sensor type. // distance sensor
Int type_rotation_vector a constant describing a rotation vector sensor type. // flip the Sensor
Int type_temperature a constant describing a temperature sensor type // temperature sensor unit: degree Celsius

Certificate ----------------------------------------------------------------------------------------------------------------------------------------------------------
The methods contained in this class are get-type methods used to obtain some attributes of the selected sensor. The sensor class generally does not need new but is obtained through the sensormanager method.

II. Introduction to sensormanager

SDK explanation: "sensormanager lets you access the device's sensors. Get an instance of this class by calling context. getsystemservice () with the argument sensor_service.
Always make sure to disable sensors you don't need, especially when your activity is paused. failing to do so can drain the battery in just a few hours. note that the system will not disable sensors automatically when the screen turns off."
Sensormanager allows you to access the sensor of the device. You can call the context. getsystemservice method by passing in the sensor_service parameter to obtain an instance of sensor. Always remember to close the sensor when you don't need it, especially when the activity is tentative. Ignoring this will consume the battery in a few hours. Note that when the screen is off, the system will not automatically turn off the sensor.

Three Common Sensors

(1) Acceleration Sensor

Three floating point models can be obtained through this sensor.

X-axis
Y-axis

Z-axis

See an illustration in Android advanced programming 2 to analyze secondary data.

X y z corresponds to values [0] to [2] respectively.

X indicates the acceleration of left and right movement.

Y indicates the acceleration of forward and backward movement.

Z represents the acceleration in the vertical direction (the test found that after the mobile phone is placed on the horizontal desktop stable, XY is 0 z and the value of 9.4 is about equal to the gravity acceleration. In turn, you can make a simpleAlgorithmIf you have time to implement a gravity dynamometer, we will give you an example)

Let's take a look at a basic acceleration demo.CodeAnnotations in

/** @ Author octobershiner * 2011 07 27 * se. hit * an example of Android acceleration sensor **/package uni. sensor; import Java. util. iterator; import Java. util. list; import android. app. activity; import android. content. context; import android. hardware. sensor; import android. hardware. sensorevent; import android. hardware. sensoreventlistener; import android. hardware. sensormanager; import android. OS. bundle; import android. util. log; P Ublic class sensordemoactivity extends activity {/** called when the activity is first created. * // set the log label Private Static final string tag = "sensor"; private sensormanager Sm; @ override public void oncreate (bundle savedinstancestate) {super. oncreate (savedinstancestate); setcontentview (R. layout. main); // create a sensormanager to obtain the system's sensor service Sm = (sensormanager) getsystemservice (context. sensor_service); // select Int sensortype = sensor. type_accelerometer;/** the most common method to register an event * parameter 1: sensoreventlistener listener * parameter 2: A sensor service may have multiple sensor implementations, call getdefasensensor to obtain the Default Sensor * parameter 3: refresh frequency of data changes in the mode optional **/SM. registerlistener (myaccelerometerlistener, SM. getdefasensensor (sensortype), sensormanager. sensor_delay_normal);}/** implementation of the sensoreventlistener interface requires two methods * method 1 onsensorchanged to be triggered when data changes * method 2 onaccuracycha Nged is called when the precision of the obtained data changes. For example, if the data cannot be obtained suddenly, **/FINAL sensoreventlistener myaccelerometerlistener = new sensoreventlistener () {// rewrite onsensorchanged method public void onsensorchanged (sensorevent) {If (sensorevent. sensor. getType () = sensor. type_accelerometer) {log. I (TAG, "onsensorchanged"); // The meaning of the three values is explained in the illustration. Float x_lateral = sensorevent. values [0]; float y_longitudinal = sensorevent. values [1]; float Z _ Vertical = sensorevent. values [2]; log. I (TAG, "\ n heading" + x_lateral); log. I (TAG, "\ n pitch" + y_longitudinal); log. I (TAG, "\ n roll" + z_vertical) ;}// rewrite onaccuracychanged method public void onaccuracychanged (sensor, int accuracy) {log. I (TAG, "onaccuracychanged") ;}}; public void onpause () {/** is critical: note that even if the activity is invisible, the sensor will continue to work. During the test, we can find that there is no normal refresh frequency * and it will be very high. Therefore, you must disable the trigger in the onpause method; otherwise, it will be time-consuming. The user consumes a large amount of power and is not responsible. **/SM. unregisterlistener (myaccelerometerlistener); Super. onpause ();}}

During the test, we will find that the refresh speed is extremely fast, which leads to a problem. If we really want to present it in the UI, we will constantly draw the interface, which is very resource-consuming, so a solution provided in Android advanced programming is to introduce a new thread to refresh the interface. If you have time tomorrow, give the example to everyone.

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.