Gravity Sensor in Android development

Source: Internet
Author: User

With a wide range of sensors in Android development, Google offers 11 sensors for use in layers: acceleration, magnetism, direction, gyroscope, light, pressure (back to current pressure), temperature, proximity (detecting distance between objects and phones), gravity, linear acceleration, and rotational vectors.
Before using the sensor, the class interface in the project needs to be Sensoreventlistener, and after the interface there will be two overloaded methods to listen and obtain the sensor status of sensing detection. where the Onaccuracychanged () method is called when sensing detects a change in the precision of the sensor, the onsensorchanged () method is called when sensing detects that the value of the sensor has changed.
After the project is established, declare Sensormanager and sensor two objects, complete the operation of the registration of the Sensor Manager, sensor and sensor through three statements:
Manager = (Sensormanager) getsystemservice (sensor_service);//Get sensor service.
Sensor = Manager.getdefaultsensor (sensor.type_accelerometer);//Get Accelerometer Service
Manager.registerlistener (this, sensor, SENSORMANAGER.SENSOR_DELAY_UI);//Register the Sensor service.
In the Sensor service registration method, there are three parameters, three parameters are: Monitor sensor event, sensor target type, delay time accuracy density (including fastest,game,ui,normal four cases, fastest too sensitive to use, Game is mainly used in the development of games, the general development of the use of UI and normal). After the registration of the sensor service is completed, the sensing data can be read.
public void Onsensorchanged (Sensorevent event)
{
MGX = event.values[sensormanager.data_x];
Mgy = event.values[sensormanager.data_y];
if (mgx<2&&mgx>-2&&mgxold<-2)
{Wifisend ("1");}
else if (mgx<2&&mgx>-2&&mgxold>2)
{Wifisend ("2");}
else if (mgy<2&&mgy>-2&&mgyold<-2)
{Wifisend ("3");}
else if (mgy<2&&mgy>-2&&mgyold>2)
{Wifisend ("4");}
Mgxold = MGX; Mgyold = mgy;
}
The function of this code is to judge the way the mobile phone rotates by comparing the data of the acceleration sensor before and after two times, and then carry on the movement control of the mobile car to achieve the purpose of the gravity Induction control trolley.

After the sensor is used, the sensor service needs to be written off in engineering to avoid consuming the phone when the activity is running the day after tomorrow.  

Sensormanager.unregisterlistener (Sensoreventlistener, msensor);

Gravity Sensor in Android development

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.