How to use the sensor for Android

Source: Internet
Author: User

1.Sensor Type
1) Gravity sensor/Accelerometer (g-sensor)
2) Light Sensing (light-sensor)
3) Temperature Sensing
4) Direction sensing
5) magnetic field
6) Proximity

2. How to implement sensor programming
A. Get system service (Sensor_service) returns a Sensormanager object
Sensormanager = (Sensormanager) getsystemseriver (Sensor_service);
B. Obtaining an object of the appropriate sensor type through the Sensormanager object
Sensorobject = Sensormanager.getdefaultsensor (sensor Type);
C. Declare a Sensoreventlistener object to listen for the sensor event and overload the Onsensorchanged method
Sensoreventlistener Sensorlistener = new Sensoreventlistener () {
};
D. Register the appropriate Sensorservice
Sensormanager.registerlistener (Sensorlistener, Sensorobject, Sensor TYPE);
E. Destruction of the corresponding Sensorservice
Sensormanager.unregisterlistener (Sensorlistener, sensorobject);

The F:sensorlistener interface is the center of the sensor application. It consists of two required methods:
The onsensorchanged (int sensor,float values[]) method is called when the sensor value changes.
This method is called only for sensors that are monitored by this application (see below for more information). The parameters of the method include: An integer indicating the change of the sensor; a floating-point value array that represents the sensor data itself. Some sensors provide only one data value, while others provide three floating-point values. Both the direction and accelerometer sensors provide three data values.
The onaccuracychanged (int sensor,int accuracy) method is called when the accuracy of the sensor changes. Parameters include two integers: one for the sensor and another for the sensor's new exact value.

3. About G-sensor
The type of Android accelerometer sensor is Sensor.type_accelerometer
Returns the accelerometer value via Android.hardware.SensorEvent.
The unit of the acceleration sensor return value is the unit of acceleration m/s^2 (meters per two seconds), and the values in three directions are
Values[0]: X-axis direction acceleration
VALUES[1]: Y-axis direction acceleration
VALUES[2]: Z-axis direction acceleration
One of the x, Y, Z directions is defined as the reference point in the lower right corner of the cell phone that is placed horizontally.
The x direction is the phone's horizontal direction, right is positive
The y direction is the cell phone's vertical direction, forward is positive
The z direction is the cell phone's vertical direction, the direction towards the sky is positive, the Earth's direction is negative
Note: The Earth's intrinsic gravitational acceleration g (value is 9.8 m/s^2),
So the actual acceleration value in reality should be the Z-direction return value-9.8 m/s^2.
For example, if you throw your phone up with a 2 m/s^2 acceleration, the return value in the z direction should be 11.8 m/s^2.
Conversely, if the mobile phone with 2 m/s^2 acceleration Falls, the z-direction of the return value should be 7.8 m/s^2.
The x, y direction does not have the above limitation.

How to use the sensor for Android

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.