Android framework Sensor

Source: Internet
Author: User

1. Android screen Conversion

Descriworientationlistener. Java

Msensormanager. getdefasensensor (sensor.Type_accelerometer); // Use g-sensor.

Rotation implementation (see G-sensor and screen rotation: http://blog.csdn.net/jaylin2013/article/details/5698652 ):

When the system starts windowmanger. Java, it starts phonewindowmanager. Java, which has an internal class myorientationlistener extended from javasworientationlistener. java.

Javasworientationlistener. Java is a helper class that is called by windowmanger. Java to receive data when the device direction changes.

Invalid worientationlistener. java is embedded in sensormanger. java is registered. It listens back to the data transmitted by G-sensor, that is, the acceleration in the direction of X, Y, and Z. After receiving the data, it is converted and processed. If the roate condition is met, it is called.

The implementation of the iwindowmanager interface is similar to the setrotation () method in windowmanagerservice. Java to achieve screen conversion.

Sensormanager obtains sensor data from the device through polling,
The sensor data structure is defined in sensor. h,

The sensormanager processes only the vector. V, vector. Status, and time fields and distributes them to registered listeners.

For example, the first item
Vector. V contains information values in the X, Y, and Z directions, which are registered by javasworientataionlister,

After sensormanager obtains these three values, it passes them to worker worientataionlister. The latter code is located:

Frameworkd/base/CORE/Java/Android/View/javasworientationlistener. Java

Windoworientataionlister calculates the orientation corresponding to the device after receiving these three values, and executes the onorientationchanged function for further uploading.

Javasworientataionlister is a pure virtual class. If you need to control the direction in the APK, You can reload an instance,

The Android system instance is in phonewindowmanager. Java and is named myorientationlistener.

Frameworks/policies/base/phone/COM/Android/Internal/policy/impl/phonewindowmanager. Java

If you want to rotate,
Myorientationlistener calls the following code to rotate the window:

Mwindowmanager. setrotation (rotation, false, mfancyrotationanimation );

G-sensor: http://blog.csdn.net/a345017062/article/details/6592527


Orientationeventlistener class

Msensor = msensormanager. getdefasensensor (sensor.Type_accelerometer);

------------------------------------------
// If portrait change to landscape

If (getrequestedorientation () = activityinfo. screen_orientation_landscape ){
Setrequestedorientation (activityinfo. screen_orientation_portrait );
}

// If landscape change to portrait

Else if (getrequestedorientation () = activityinfo. screen_orientation_portrait ){
Setrequestedorientation (activityinfo. screen_orientation_landscape );
}

However, the above method has a problem, that is, you must set a default screen direction, that is, I must be in androidmanifest. in XML, you can configure a default screen direction. For example, add the following configuration: Android: screenorientation = "portrait", otherwise it will become invalid, the getrequestedorientation method does not necessarily show the current real screen direction. It seems that it is only the default direction in the configuration file.

What if I want to get the current screen direction? Or how much angle do I need to know about the fact detection? In fact, there is another method. You can use orientationeventlistener to obtain the current screen Rotation Angle in real time. Based on the angle, you can determine whether the screen is landscape screen or landscape screen, by Rewriting onorientationchanged, you can get the angle value and perform corresponding processing.
Public void onorientationchanged (INT orientation ){
// Todo auto-generated method stub
If (orientationeventlistener. orientation_unknown! = Orientation ){
If (0 = orientation)
Log. E ("mynewlog", "not change the screen !! ");
Else if (90 = orientation)
Log. E ("mynewlog", "left side is at the top ");
Else if (180 = orientation)
Log. E ("mynewlog", "upside down ");
Else if (270 = orientation)
Log. E ("mynewlog", "right side is to the top ");
}
}
Conversion from: Android screen direction Conversion

Toolsensor-how to use the underlying API of the operating Sensor

--------------------------------------

About each sensor:
G-Sensor

The android accelerometer is of the sensor. type_accelerometer type.
Return the value of the accelerometer through Android. Hardware. sensorevent.
The Unit returned by the accelerometer is the unit of acceleration m/s ^ 2 (meter per second). The values in three directions are

Values [0]: X-axis Acceleration
Values [1]: acceleration in Y-axis direction
Values [2]: acceleration in the z-axis direction

The X, Y, and Z directions are defined as the coordinates of the reference system at the bottom right of the horizontally placed mobile phone.

The Direction X is the horizontal direction of the mobile phone, and the right is positive
The Y direction is the horizontal vertical direction of the mobile phone, and the front is positive
The Y direction is the vertical direction of the mobile phone space, the direction of the sky is positive, and the direction of the earth is negative

Note that:
Due to the inherent gravity acceleration g (the value is 9.8 Mb/s ^ 2 ),
Therefore, in reality, the actual acceleration value should be the Z-direction return value-9.8 m/s ^ 2.
For example, if you throw a mobile phone at the acceleration of 2 Mb/s ^ 2, then the return value in the z direction is 11.8 Mb/s ^ 2.
Otherwise, if the mobile phone drops at a 2 Mbit/s ^ 2 acceleration, the return value in the z direction should be 7.8 Mbit/s ^ 2.
In the X and Y directions, there is no such restriction.

--------------------------------------------

Let's first briefly introduce the three-dimensional coordinate system of smartphone:

X-axis: horizontal. A positive value indicates the right and a negative value indicates the right;

Y-axis: vertical, positive representing the top, negative representing the bottom;

Z-axis: pointing to the front-end interface of the mobile phone screen.

The following shows the running result of the real machine:

From: http://www.linuxidc.com/Linux/2011-08/41926p2.htm

2. Android magnetic

The framework layer does not directly call type_magnetic_field. It should be called directly by the application.

3. Android Orientation

The framework layer does not directly call type_orientation. It should be called directly by the application.


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.