Android Sensor usage, androidsensor

Source: Internet
Author: User

Android Sensor usage, androidsensor

Use of sensor Senso in Android

1. Sensor Type

Android has a variety of sensors. Currently, Android SDK supports light sensors, rotating vector sensors, pressure sensors, gyroscope sensors, acceleration sensors, Gravity Sensors, direction sensors, magnetic field sensors, short-range sensors. However, not all mobile phones have all sensors. Generally, high-end mobile phones have most sensors. After all, sensors require money and the price is naturally high.

2. Practical Application of Sensor

In Android development, how does one use sensors to add sensor functions to Android applications? For example, by means of a shake, a sensor is triggered to search for nearby users; for example, when the phone is close to the ear, the phone will be black, which means the light sensor works.

3. Steps for using Sensor

  • Get sensor management object SensorManager
  • Class for creating sensor event listening. This class must implement the android. hardware. SensorEventListner interface.
  • Use the SensorManager. registerListener method to register the specified sensor.

4. Sensor event Interface

The onSensorChanged () and onAccuracyChanged () Methods of the SensorEventListner interface are used to process sensor events.

1 public interface SensorEventListener {2 3 // Sensor data change call 4 public void onSensorChanged (SensorEvent event); 5 // call 6 public void onAccuracyChanged (sensor Sensor, int accuracy); 7}

5. Simple code implementation

Define SensorManager

1 SensorManager sensorManager=(SensorManager) getSystemService(SENSOR_SERVICE);

Define an anonymous internal class or define a class to implement the SensorEventListner Interface

 1 private SensorEventListener sensorEventListener=new SensorEventListener() { 2          3         @Override 4         public void onSensorChanged(SensorEvent event) { 5              6              float[] values=event.values; 7              float x = values[0];  8              float y = values[1];  9              float z = values[2];     10         }11         @Override12         public void onAccuracyChanged(Sensor sensor, int accuracy) {13             14         }15     };

Register and disable sensor listening events

@Override    protected void onResume() {        super.onResume();        if(sensorManager!=null){            sensorManager.registerListener(sensorEventListener, sensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER), SensorManager.SENSOR_DELAY_NORMAL);                }    }    @Override    protected void onPause() {        super.onPause();        if(sensorManager!=null){            sensorManager.unregisterListener(sensorEventListener);        }    }

 


Android sensor, I use my mobile phone to debug the program. With SensorTYPE_ALL, I can get 6 sensors, but I actually register each sensor.

Several other sensors do not exist physically, but the system is virtual, so there is no value.

How to obtain sensor data at intervals in Android applications

MySensorManager. registerListener (// register the listener
MySensorListener, // listener SensorListener object
SensorManager. SENSOR_ACCELEROMETER, // The sensor type is acceleration.
SensorManager. SENSOR_DELAY_UI); // frequency
Let's take a look at its parameters.

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.