[Based Android] Android sensor introduction (1) 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 accelerometer 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.

// Ray sensor unit Lux 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 value of the three coordinate axes micro tesla

Int type_orientation this constant is deprecated. Use sensormanager. getorientation () instead.

// When the sensor is out of date, you can use this method to obtain it.

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 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

 1   /*  
2 * @ Author octobershiner
3 * 2011 07 27
4 * Se. Hit
5 * An example of Android Acceleration Sensor
6 * */
7
8 Package Uni. sensor;
9
10 Import Java. util. iterator;
11 Import Java. util. List;
12
13 Import Android. App. activity;
14 Import Android. content. context;
15 Import Android. Hardware. sensor;
16 Import Android. Hardware. sensorevent;
17 Import Android. Hardware. sensoreventlistener;
18 Import Android. Hardware. sensormanager;
19 Import Android. OS. Bundle;
20 Import Android. util. log;
21
22 Public Class Sensordemoactivity Extends Activity {
23 /** Called when the activity is first created. */
24 // Set log labels
25 Private Static Final String tag = "sensor ";
26 Private Sensormanager Sm;
27 @ Override
28 Public Void Oncreate (bundle savedinstancestate ){
29 Super . Oncreate (savedinstancestate );
30 Setcontentview (R. layout. Main );
31 // Create a sensormanager to obtain the system's sensor Service
32 Sm = (sensormanager) getsystemservice (context. sensor_service );
33 // Select an acceleration sensor
34 Int Sensortype = sensor. type_accelerometer;
35
36 /*
37 * The most common method for registering an event
38 * Parameter 1: sensoreventlistener listener
39 * Parameter 2: A sensor service may have multiple sensor implementations. Call getdefasensensor to obtain the Default Sensor.
40 * Parameter 3: The refresh frequency of optional data changes in the mode.
41 * */
42 SM. registerlistener (myaccelerometerlistener, Sm. getdefasensensor (sensortype), sensormanager. sensor_delay_normal );
43
44 }
45
46 /*
47 * Two methods are required to implement the sensoreventlistener interface.
48 * Method 1 onsensorchanged is called when data changes
49 * Method 2 onaccuracychanged is called when the precision of the obtained data changes. For example, if the data cannot be obtained suddenly
50 * */
51 Final Sensoreventlistener myaccelerometerlistener = New Sensoreventlistener (){
52
53 // Onsensorchanged Method
54 Public Void Onsensorchanged (sensorevent ){
55 If (Sensorevent. sensor. GetType () = sensor. type_accelerometer ){
56 Log. I (TAG, "onsensorchanged ");
57
58 // The meaning of the three values has been explained in the illustration.
59 Float X_lateral = sensorevent. Values [0];
60 Float Y_longitudinal = sensorevent. Values [1];
61 Float Z_vertical = sensorevent. Values [2];
62 Log. I (TAG, "\ n heading" + x_lateral );
63 Log. I (TAG, "\ n pitch" + y_longitudinal );
64 Log. I (TAG, "\ n roll" + z_vertical );
65 }
66 }
67 // Onaccuracychanged Method
68 Public Void Onaccuracychanged (sensor, Int Accuracy ){
69 Log. I (TAG, "onaccuracychanged ");
70 }
71 };
72
73 Public Void Onpause (){
74 /*
75 * Important: note that even if the activity is invisible, the sensor will continue to work. During testing, we can find that there is no normal refresh frequency.
76 * It will also be very high, so you must disable the trigger in the onpause method. Otherwise, it will consume a lot of User power and will not be responsible.
77 * */
78 SM. unregisterlistener (myaccelerometerlistener );
79 Super . Onpause ();
80 }
81
82 }

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.

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.