Gravity inductive usage analysis of Android programming _android

Source: Internet
Author: User
Tags sqlite database

This example describes the gravity-inductive usage of Android programming. Share to everyone for your reference, specific as follows:

Gravity sensors rely mainly on the mobile phone's accelerometer (accelerometer) to achieve

There are eight sensors in the development of Android, but not every real machine supports these sensors. Because many functional users are not care, it is possible that developers will be able to disable certain functions. or according to the actual situation of the real machine to do development, the following main discussion of the acceleration sensor implementation, the sensor name is as follows:

Acceleration Sensor (Accelerometer)
Gyroscope sensor (gyroscope)
Ambient lighting sensor (light)
Magnetic sensors (magnetic field)
Directional sensor (orientation)
Pressure transducer (pressure)
Distance sensor (proximity)
Temperature sensor (temperature)

1.SensorMannager Sensor Management Object

All the sensors on the phone need to be accessed via Sensormannager, and the Getsystemservice (Sensor_service) method can be used to get the sensor management object of the current cell phone.

2. Implement Sensoreventlistener interface

We need to implement the Sensoreventlistener interface Onsensorchanged (sensoreventevent) method to capture the status of the cell phone sensor and get the weight of the three-direction x-axis Z axis of the cell phone. With this three-direction data, the principle of gravitational induction we've learned.

public void onsensorchanged (Sensorevent e) {
  float x = e.values[sensormanager.data_x];
  Float y = e.values[sensormanager.data_y];
  float z = e.values[sensormanager.data_z];
}

As shown in the preceding code: the range of values for float x y Z in 3 directions is between 10 and 10. The following explains the meaning of the x-axis y-Axis gravity component (note here is the origin of the coordinates: positive to the sky, negative to the ground, just as opposed to the programming-time coordinates):

(1) The cell phone screen to the left side, the x axis toward the sky, vertical placement, this time the Y axis and the z axis does not have the gravity component, because the x-axis is facing the sky, so its gravity is the largest. At this time the x axis, y axis, Z axis of the value of the gravity component (10,0,0);

(2) The cell phone screen to the right side, the x-axis toward the ground, vertical placement, this time the Y axis and the z axis does not have the gravity component, because the x-axis is facing the ground so its gravity component is the smallest. At this time the x axis, y axis, Z axis of the value of the gravity component is ( -10,0,0);

(3) Mobile phone screen vertical placement, the y-axis toward the sky, vertical placement, this time the x axis and the z axis does not have the gravity component, because the Y axis toward the sky so its gravity is the largest. At this time the x axis, y axis, Z axis of the value of the gravity component (0,10,0);

(4) Mobile phone screen vertical placement, the y-axis on the ground, vertical placement, this time the x-axis and the z-axis does not have the gravity component, because the y-axis towards the ground so that its gravity is the smallest. At this time the x axis, y axis, Z axis of the value of the gravity component (0,-10,0);

(5) Mobile phone screen up, Z axis toward the sky, horizontally placed, this time the X axis and the Y axis does not have the gravity component, because the z axis is facing the sky, so its gravity component is the largest. At this time the x axis, y axis, Z axis of the value of the gravity component (0,0,10);

(6) Mobile phone screen up, z-axis on the ground, horizontally placed, this time the X-axis and the y-axis does not have the gravity component, because the z-axis toward the ground so that its weight is the smallest. The values of the gravity components of the x, y, and Z axes are (0,0,-10) respectively.

3. Register Sensoreventlistener

Use the Sensormannager call Getdefaultsensor (Sensor.type_accelerometer) method to get the Sensor object of accelerated gravity induction. Because I'm talking about the gravitational acceleration sensor, so the parameter is Sensor.type_accelerometer, and if you need to get another sensor, you need to pass in the corresponding name. Use the Sensormannager call Registerlistener () method to register, the third parameter is the sensitive precision of detection, according to different requirements to choose the accuracy, game development recommend the use of Sensormanagersensor_delay_ GAME.

4. The way of gravity induction simple velocity calculation

Every time you shake your phone, calculate the gravitational component of the x-axis y-axis Z axis, and then you can record the weight of each swing and the weight of the previous gravitational component can be compared, using the difference and time to calculate their speed of movement.

The gravitational induction device comprises three parts of the sensor, the processor and the controller. The sensor is responsible for detecting the state of the memory, calculating the gravitational acceleration value of the memory, and the processor determining whether the acceleration value is beyond the safe range, while the controller is responsible for controlling the head locking or releasing the safe berth. Once the sensor detects and is judged by the processor that the current acceleration of gravity exceeds the safety value, the controller will stop reading and writing through the hardware control head and quickly return to the location, locked in the proprietary head parking area. This sequence of actions will be completed in 200 milliseconds. When the sensor detects that the acceleration is restored to its normal range, the product resumes its work.

The code for the Android multimedia framework is in the following directory: External/opencore/. This directory is the root directory of the Android multimedia framework, which contains subdirectories as follows:

* Android: This is an upper-level library that implements a player and author for Android based on the Pvplayer and Pvauthor SDK
* Baselibs: The underlying library containing data structure and line Cheng, etc.
* CODECS_V2: This is a more content library, mainly contains the implementation of codec, and a OpenMAX implementation
* Engines: Contains Pvplayer and Pvauthor engine implementations
*EXTERN_LIBS_V2: Header file containing Khronos OpenMAX
*fileformats: File format According to the specific resolution (parser) class
* Nodes: Each node class for codec and file resolution
* OSCL: Operating system compatible library
* PVMI: Abstract interface for input/output control
* Protocols: Mainly related to network-related RTSP, RTP, HTTP and other protocols
* Pvcommon:pvcommon library file Android.mk files, no source files
*pvplayer:pvplayer library file Android.mk files, no source files
* Pvauthor:pvauthor library file Android.mk files, no source files
* TOOLS_V2: Compile tools and some modules that can be registered

Here is a partial test code:

Private Sensormanager sensormgr;
Sensor Sensor = Sensormgr.getdefaultsensor (Sensor.type_accelerometer);
Save the coordinates of X y Z last time float bx = 0;
float by = 0;
float BZ = 0;
Long btime = 0;//this time Sensormgr = (Sensormanager) getsystemservice (Sensor_service); Sensoreventlistener LSN = new Sensoreventlistener () {public void onsensorchanged (Sensorevent e) {float x = E.value
    S[SENSORMANAGER.DATA_X];
    Float y = e.values[sensormanager.data_y];
    float z = e.values[sensormanager.data_z];  To calculate the value of X Y Z, you can calculate the shaking speed by this value//speed = distance/time//x axis speed float SPEADX = (X-BX)/(System.currenttimemillis ()-
    Btime);
    Y-axis velocity float Speady = (y-by)/(System.currenttimemillis ()-btime);
    Z-axis velocity float Speadz = (Z-BZ)/(System.currenttimemillis ()-btime);  This simple speed can be calculated, if you want to calculate the acceleration can also, in the kinematics, acceleration A and speed,//displacement are Related: vt=v0+at,s=v0*t+1/2at^2, s= (vt^2-v0^2)/(2a), according to this information can also solve a BX
    = x;
    by = y;
    BZ = Z;
  Btime = System.currenttimemillis (); } public void onAccuracychanged (Sensor s, int accuracy) {}};

 Register Listener, the third parameter is the accuracy of the detection Sensormgr.registerlistener (LSN, sensor, sensormanager.sensor_delay_game);

More interested readers of Android-related content can view the site: Android Development Primer and Advanced tutorials, the Android View View tips Summary, the activity tips summary of Android programming, Android Operation SQLite Database skills Summary, "Android operation JSON format Data Skills summary", "Android Database Operation skills Summary", "Android File Operation skills Summary", "Android programming development of SD card operation method Summary", " Android Resource Operation tips Summary and the "Android Controls usage Summary"

I hope this article will help you with the Android program.

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.