Android sensor use

Source: Internet
Author: User

The use of the Android sensor is accelerometer, direction sensor, magnetic field, pressure, temperature, light sense. The special distance sensor is introduced as an example.

The first class needs implement Sensoreventlistener. The use of sensors is divided into the following steps:

1. Create a new Sensormanager to manage sensor services. The code is:

Sensormanager smg= (Sensormanager) Getsystemservice (Context.sensor_service);

2. Call Sensormanager's Registerlistener () in the activity's Onresume () method to register the sensor's Listener (Onresume () is called when the activity is resumed). The Registerlsitener () has three parameters, the listener for the Sensoreventlistener Monitor sensor event, the sensor object, and the rate for acquiring the sensor data. The rate has three kinds: sensor_delay_fasetst: The fastest, the delay is small, consumes electricity. Sensor_delay_game: Suitable for game frequency, commonly used. Sensor_delay_normal: Normal frequency. SENSOR_DELAY_UI: Suitable for normal user frequency.

3. In onsensorchanged (Sensorevent event), write how the sensor data changes are handled. This is the method that the Sensoreventlistener interface must implement when the sensor value change is the callback method.

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.