The gravity sensor developed by Android

Source: Internet
Author: User

The gravity sensor is similar to the direction sensor development step, as long as the value of x, Y and Z in the mid-term can be programmed according to their changes, first look at a diagram

Assuming that the local gravitational acceleration value is g

When the phone is facing up, the z value is Q, and the z value is-g

When the right side of the phone face up, the value of X is G, the right side faces up, the value of X is-G

When the phone is facing up on the side, Y is the value of G, the right side facing up, Y is the value of-g

After understanding the meaning of x, Y and z in the gravity sensor, let's start learning how to use

First we create a sensor manager and a sensor listener that managers use to manage sensors and create a wide variety of sensors that monitor sensor changes and operate accordingly.

Private Sensormanager Sensormanager;

Private Mysensoreventlistener Mysensoreventlistener;

Mysensoreventlistener= new Mysensoreventlistener (); //This listener is, of course, defined by ourselves, and when the gravity sensor detects a change in the position of the mobile phone, we can take the appropriate action, where the value of x, Y, Z is printed out tightly .

Private Final class Mysensoreventlistener implements sensoreventlistener{

@Override

Can get the change value of the sensor in real-time measurement

public void Onsensorchanged (Sensorevent event) {

Gravity sensor

if (Event.sensor.getType () ==sensor.type_accelerometer) {

float x = event.values[sensormanager.data_x];

Float y = event.values[sensormanager.data_y];

float z = event.values[sensormanager.data_z];

Tv_accelerometer is a TextView label on the interface, no longer repeat

Tv_orientation.settext ("Orientation:" +x+ "," +y+ "," +z ");

}

}

We create a gravity sensor in the Onresume method and register the listener with the system

protected void Onresume () {

Sensor sensor_accelerometer=sensormanager.getdefaultsensor (Sensor.type_accelerometer);

Sensormanager.registerlistener (Mysensoreventlistener,sensor_accelerometer, sensormanager.sensor_delay_ui);

Super.onresume ();

}

Finally we log off all sensors in OnPause () and release the Gravity sensor resources!

protected void OnPause () {

/logout of all sensor monitors

Sensormanager.unregisterlistener (Mysensoreventlistener);

Super.onpause ();

}

Here, the introduction of the gravity sensor is complete!

The gravity sensor developed by 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.