Android gravity sensor Programming

Source: Internet
Author: User
Add the dosomething (x, y, z) function called by the handler function when the listener is created when the gravity changes. This method is called by the listener when the phone skew changes. What we need to do is to fill in this method for our own processing when gravity changes. When x = y = 0, the mobile phone is placed horizontally. When x = 0 and Y> 0, the horizontal position on the top of the mobile phone is greater than the bottom, that is, the status of the mobile phone when the phone is generally answered. When x = 0 and Y <0, the horizontal position on the top of the mobile phone is smaller than the bottom. Mobile phones are rarely in this status. When y = 0 and x> 0, the horizontal position on the right side of the mobile phone is greater than that on the left side, that is, the right side is lifted. When y = 0 and x <0, the horizontal position on the right side of the mobile phone is smaller than that on the left side, that is, the left side is lifted. When z = 0, the cell phone plane is perpendicular to the horizontal plane. When Z is greater than 0, the screen of the mobile phone is facing up. When Z is less than 0, the phone screen is down. // 1. Obtain the Sensor Object of the gravity sensor // define the following member variables in the activity: Private sensormanager mmanager = NULL; private sensor msensor = NULL; // The following Code Added to the oncreate () method: mmanager = (sensormanager) getsystemservice (context. sensor_service); msensor = manager. getdefasensensor (sensor. type_accelerometer); // 2. Create a listener // define the following member variable in the activity: Private sensoreventlistener mlistener = NULL; // Add the following code to the oncreate () method: mlistener = new sensoreventlistener () {public void onaccuracychanged (sensor, int accuracy) {} public void onsensorchanged (sensorevent event) {float x = event. values [sensormanager. data_x]; float y = event. values [sensormanager. data_y]; float z = event. values [sensormanager. data_z]; dosomething (x, y, z) ;}}; // 3. register the listener // Add the following code to the onresume () method: mmanager. registerlistener (mlistener, msensor, sensormanager. sensor_delay_game); // here sensor_delay_game can also be the following constant: // else // sensor_delay_ui // sensor_delay_normal // 4. Cancel the listener // Add the following code to the onpause () method: mmanager. unregisterlistener (mlistener );
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.