Use of sensor sensors in Android

Source: Internet
Author: User

Use of sensor Senso in Android

1.Sensor type

Android has a variety of sensors, the current Android SDK support sensors are: light sensor, rotation vector sensor, pressure sensor, gyroscope sensor, accelerometer, gravity sensor, direction sensor, magnetic field sensor, proximity sensor and so on. But not all mobile phones have all the sensors, the general high-end mobile phones with most sensors, after all, the sensor needs money, the price is naturally expensive.

2.Sensor Practical Application

So in Android development, how to use sensors to add sensor functionality to Android applications, such as shaking, by accelerating the sensor, triggering the search for nearby people, for example, each time you answer the phone, when the phone close to the ear, the hand of the black screen, this is the light sensor function.

3.Sensor Use steps

    • Get the Sensor management object Sensormanager
    • The class that creates the sensor event listener, which must implement the Android.hardware.SensorEventListner interface
    • Registering the specified sensor using the Sensormanager.registerlistener method

4. Sensor Event Interface

Sensoreventlistner interface, the onsensorchanged () and onaccuracychanged () methods of this interface are used to handle the corresponding sensor events

1  Public Interface Sensoreventlistener {23     //Sensing data change call 4public     void onsensorchanged (Sensorevent event); 5     When sensor accuracy changes, call 6public     voidint  accuracy);     7 }

5. Simple Code Implementation

Define Sensormanager

1 sensormanager sensormanager= (Sensormanager) Getsystemservice (Sensor_service);

Define an anonymous inner class or define a class to implement the Sensoreventlistner interface

1 PrivateSensoreventlistener sensoreventlistener=NewSensoreventlistener () {2         3 @Override4          Public voidonsensorchanged (Sensorevent event) {5             6              float[] values=event.values;7              floatx = values[0]; 8              floaty = values[1]; 9              floatz = values[2]; Ten         } One @Override A          Public voidOnaccuracychanged (sensor sensor,intaccuracy) { -              -         } the};

Registering and shutting down Sensor listener events

@Overrideprotected voidOnresume () {Super. Onresume (); if(sensormanager!=NULL) {Sensormanager.registerlistener (Sensoreventlistener, Sensormanager.getdefaultsensor (Sensor.TYPE_ACCELEROM                Eter), sensormanager.sensor_delay_normal); }} @Overrideprotected voidOnPause () {Super. OnPause (); if(sensormanager!=NULL) {Sensormanager.unregisterlistener (Sensoreventlistener); }    }

Use of sensor sensors in 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.