SoFunction
Updated on 2025-03-01

Analysis of application method of gravity sensing in Android programming

This article describes the application method of gravity sensing in Android programming. Share it for your reference, as follows:

Gravity sensing is mainly achieved by relying on the acceleration sensor of the mobile phone.

There are eight sensors in Android development, but not every real machine supports these sensors. Because many functions are not careful at all, developers may block certain functions. It is still necessary to develop according to the actual situation of the real machine. The following mainly discusses the specific implementation method of the acceleration sensor. The sensor name is as follows:

Accelerometer
Gyroscope sensor (gyroscope)
Ambient light sensor (light)
Magnetic field
Direction sensor (orientation)
Pressure sensor (pressure)
Distance sensor (proximity)
Temperature sensor (temperature)

Sensor management objects

All sensors in the mobile phone need to be accessed through SensorMannager. Call the getSystemService (SENSOR_SERVICE) method to get the sensor management object of the current mobile phone.

2. Implement the SensorEventListener interface

We need to implement the SensorEventListener interface onSensorChanged(SensorEventivevent) method to capture the state of the mobile phone sensor and get the gravity component in the three directions of the mobile phone X-axis Y-axis Z-axis. With the principle of data gravity sensing in these three directions, we have learned it.

public void onSensorChanged(SensorEvent e) {
  float x = [SensorManager.DATA_X];
  float y = [SensorManager.DATA_Y];
  float z = [SensorManager.DATA_Z];
}

As shown in the above code: the value range of float x y z in the 3 directions is between -10 and 10. The following is an explanation of the meaning of the gravity component of the X-axis Y-axis Z-axis (it should be noted here that the coordinate origin is: positive to the sky and negative to the ground, which is exactly the opposite of the coordinates when programming):

(1) The phone screen is facing the left side, and the X-axis is facing the sky and placed vertically. At this time, the Y-axis and Z-axis have no gravity component, because the X-axis faces the sky, so its gravity component is the largest. At this time, the values ​​of gravity components of the X-axis, Y-axis, and Z-axis are (10, 0, 0);

(2) The mobile phone screen is facing the right side, and the X-axis is placed vertically toward the ground. At this time, the Y-axis and Z-axis have no gravity components, because the X-axis is facing the ground, so its gravity component is the smallest. At this time, the values ​​of gravity components of the X-axis, Y-axis, and Z-axis are (-10, 0, 0);

(3) The mobile phone screen is placed vertically and vertically, and the Y axis is facing the sky, and vertically. At this time, the X axis and the Z axis have no gravity component, because the Y axis is facing the sky, so its gravity component is the largest. At this time, the values ​​of gravity components of the X-axis, Y-axis, and Z-axis are (0, 10, 0);

(4) The mobile phone screen is placed vertically and vertically, and the Y axis is facing the ground and is placed vertically. At this time, there is no gravity component between the X axis and the Z axis. Because the Y axis is facing the ground, its gravity component is the smallest. At this time, the values ​​of gravity components of the X-axis, Y-axis, and Z-axis are (0, -10, 0);

(5) The mobile phone screen is upward, and the Z axis is facing the sky and placed horizontally. At this time, the X axis and the Y axis have no gravity component, because the Z axis is facing the sky, so its gravity component is the largest. At this time, the values ​​of gravity components of the X-axis, Y-axis, and Z-axis are (0, 0, 10);

(6) The mobile phone screen is upward, and the Z axis is placed horizontally toward the ground. At this time, the X axis and the Y axis have no gravity component, because the Z axis is facing the ground, so its gravity component is the smallest. At this time, the values ​​of gravity components of the X-axis, Y-axis, and Z-axis are (0, 0, -10) respectively.

3. Register SensorEventListener

Use SensorMannager to call the getDefaultSensor(Sensor.TYPE_ACCELEROMETER) method to get the Sensor object that accelerates gravity sensing. Because I am talking about gravity acceleration sensor, the parameter is Sensor.TYPE_ACCELEROMETER. If you need to get other sensors, you need to pass in the corresponding name. Use SensorMannager to call registerListener() method to register. The third parameter is the sensitivity and accuracy of the detection. The accuracy is selected according to different needs. It is recommended to use SensorManagerSENSOR_DELAY_ GAME for game development.

4. Simple way to calculate the speed of gravity sensing

Each time you shake the phone, you can calculate the gravity components of the X-axis, Y-axis, and Z-axis, and record them. Then, the gravity components of each shake are compared with the previous gravity components, and the difference and time can be used to calculate their movement speed.

The gravity sensing device includes three parts: a sensor, a processor and a controller. The sensor is responsible for detecting the status of the memory and calculating the gravity acceleration value of the memory; the processor judges whether the acceleration value exceeds the safe range; and the controller is responsible for controlling the locking of the magnetic head or releasing the safe parking area. Once the sensor detects and determines that the current gravity acceleration exceeds the safety value by the processor, the controller will control the head to stop reading and writing through hardware, and quickly return to the position, locking in the proprietary head parking area. This series of actions will be completed in 200 milliseconds. The product will not resume operation after the sensing device detects that the acceleration value returns to the normal value range.

The code of Android multimedia framework is in the following directory: external/opencore/. This directory is the root directory of Android multimedia framework, and the subdirectories contained in it are as follows:

* android: This is an upper-level library, which implements a Player and Author for Android based on the SDK of PVPlayer and PVAuthor.
* baselibs: The underlying library that contains data structures and thread safety, etc.
* codecs_v2: This is a library with a lot of content, mainly including codec implementations, as well as an OpenMAX implementation.
* engines: The implementation of PVPlayer and PVAuthor engines are included
*extern_libs_v2: The header file of OpenMAX containing khronos
*fileformats: The file format is parser class
* nodes: various node classes for codec and file parsing
* oscl: Operating system compatibility library
* pvmi: Abstract interface for input and output control
* protocols: mainly related to the network-related RTSP, RTP, HTTP and other protocols
* pvcommon: The file of the pvcommon library file, no source file
*pvplayer: The file of the pvplayer library file, no source file
* pvauthor: The file of the pvauthor library file, no source file
* tools_v2: Compilation tools and some registrable modules

Here are some test codes:

private SensorManager sensorMgr;
Sensor sensor = (Sensor.TYPE_ACCELEROMETER);
//Save the coordinates of the last x y zfloat bx = 0;
float by = 0;
float bz = 0;
long btime = 0;//This timesensorMgr = (SensorManager) getSystemService(SENSOR_SERVICE);
SensorEventListener lsn = new SensorEventListener() {
  public void onSensorChanged(SensorEvent e) {
    float x = [SensorManager.DATA_X];
    float y = [SensorManager.DATA_Y];
    float z = [SensorManager.DATA_Z];
    // Calculate the value of X Y Z and you can calculate the shaking speed based on this value.    //Speed ​​= distance/time    //X-axis speed    float speadX = (x - bx) / (() - btime);
    //The y-axis speed    float speadY = (y - by) / (() - btime);
    // z-axis speed    float speadZ = (z - bz) / (() - btime);
    //This simple velocity can be calculated. If you want to calculate acceleration, it is also OK. In kinematics, acceleration a and velocity,    //The displacement is related: Vt=V0+at, S=V0*t+1/2at^2, S=(Vt^2-V0^2)/(2a), based on this information, a can also be solved    bx = x;
    by = y;
    bz = z;
    btime = ();
  }
  public void onAccuracyChanged(Sensor s, int accuracy) {
  }
};
// Register the listener, the third parameter is the accuracy of detection(lsn, sensor, SensorManager.SENSOR_DELAY_GAME);

For more information about Android related content, please check out the topic of this site:Android development introduction and advanced tutorial》、《Android View View Tips Summary》、《Android programming activity operation skills summary》、《Summary of Android's SQLite database skills》、《Summary of Android data skills for operating json format》、《Android database operation skills summary》、《Android file operation skills summary》、《A summary of SD card operation methods for Android programming and development》、《Android resource operation skills summary"and"Android control usage summary

I hope this article will be helpful to everyone's Android programming design.