Basic tutorial for Android -- 10.10 sensor topic (1) -- Introduction

Source: Internet
Author: User

Basic tutorial for Android -- 10.10 sensor topic (1) -- Introduction
Basic tutorial for Android -- 10.10 sensor topic (1) -- Introduction

Tags (separated by spaces): basic Android tutorial

1. Sensor introduction:

Speaking of sensors, I believe everyone will be familiar with it. For example, an acceleration sensor is used for shaking;
Sensor DefinitionA physical device or biological organ that can detect and feel external signals and physical conditions (such as light, heat,
Moderate) or chemical composition (such as smoke), and transmit the information to other devices or organs!
Sensor types: The sensor can be divided from different angles, the conversion principle (the basic physical or chemical sensor work)
Effect); purpose; output signal and preparation of materials and processes. It is generally divided by the original working hours: physical sensors and chemical sensors
Two types! Basically, mobile phones are equipped with physical sensors. The sensors on mobile phones include the following:

Orientation sensor) Accelerometer sensor) Gyroscope sensor) Magnetic field sensor) Proximity sensor) Light sensor) Pressure sensor) Temperature sensor) Gravity sensor (introduced in Android 2.3) Linear acceleration sensor (introduced in Android 2.3) Rotation vector sensor (Android 2.3) Relative humidity sensor (Relative humidity sensor, Android 4.0) Near-Field Communication (NFC) sensor (introduced in Android 2.3). NFC is different from other sensors and has read and write functions.

Of course, in addition to these, there are also heart rate sensors, step sensors, fingerprint sensors, etc,
For more information about Sensor types supported by Android devices, see the official Sensor Summary section ~

2. How can I check the sensors supported by my mobile phone?

As mentioned above, not all mobile phones are equipped with the sensor types mounted on each mobile phone.
And the number may be different. For example, the Nexus 5 supported sensor types in my hands are:
Gravity, light, distance, pressure, and gyroscope! In addition, in the second generation of moto, gravity and light were detected,
Distance and infrared sensors! You can evaluate the sensor types supported by your mobile phone.
Website: Zhongguancun Mobile Online, Pacific, etc. Search for your own model to view related parameters!
Of course, you can also write code to see the sensor types supported by your mobile phone ~

Sample Code:

Run:

Code Implementation:

Activity_main.xml:


      
           
        
    
   
  

MainActivity. java:

Public class MainActivity extends AppCompatActivity {private TextView txt_show; private SensorManager sm; @ Override protected void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); setContentView (R. layout. activity_main); sm = (SensorManager) getSystemService (Context. SENSOR_SERVICE); txt_show = (TextView) findViewById(R.id.txt _ show); List
  
   
AllSensors = sm. getSensorList (Sensor. TYPE_ALL); StringBuilder sb = new StringBuilder (); sb. append (this phone has + allSensors. size () + sensors, respectively :); for (Sensor s: allSensors) {switch (s. getType () {case Sensor. TYPE_ACCELEROMETER: sb. append (s. getType () + Accelerometer (Accelerometer sensor) +); break; case Sensor. TYPE_GYROSCOPE: sb. append (s. getType () + Gyroscope sensor (Gyroscope Sensor) +); break; case sensor. TYPE_LIGHT: sb. append (s. getType () + Light sensor +); break; case Sensor. TYPE_MAGNETIC_FIELD: sb. append (s. getType () + Magnetic field sensor (Magnetic field Sensor) +); break; case sensor. TYPE_ORIENTATION: sb. append (s. getType () + Orientation sensor +); break; case Sensor. TYPE_PRESSURE: sb. append (s. getType () + Pressure sensor (Pressure Sensor) +); break; case sensor. TYPE_PROXIMITY: sb. append (s. getType () + distance sensor (Proximity Sensor) +); break; case sensor. TYPE_TEMPERATURE: sb. append (s. getType () + Temperature sensor (Temperature sensor) +); break; default: sb. append (s. getType () + other sensors +); break;} sb. append (device name: + s. getName () + device version: + s. getVersion () + Supplier: + s. getVendor () +);} txt_show.setText (sb. toString ());}}
  
3. Sensor-related methods and usage routines

From the example in 2, we can summarize how to obtain the Sensor and some information about the Sensor.
The process is as follows:

1) Sensor related methods

Step 1: Obtain the sensor Manager:

SensorManager sm = (SensorManager)getSystemService(SENSOR_SERVICE); 
Step 2: Obtain the Sensor Object List of the device:
List
  
    allSensors = sm.getSensorList(Sensor.TYPE_ALL);
  
Step 3: Iteratively obtain the Sensor object, and then call the corresponding method to obtain information about the Sensor:
For (Sensor s: allSensors) {sensor. getName (); // obtain the sensor name sensor. getType (); // obtain the sensor type sensor. getVendor (); // obtain the sensor supplier sensor. getVersion (); // obtain the sensor version sensor. getResolution (); // obtain the sensor value. getMaximumRange (); // obtain the maximum sensor range. getPower (); // the power consumption of the sensor during use}
2) sensor usage routine

Generally, we seldom directly obtain the Sensor and then obtain the above information! Because there is nothing
The main function is to obtain the data collected by the sensor, for example, to obtain the current atmospheric pressure,
Or the value of the orientation sensor's three corners, or the value of the gyroscope ~ Most of the sensor data is collected
The following routine:

~ Step 1: Obtain the sensor Manager:

SensorManager sm = (SensorManager)getSystemService(SENSOR_SERVICE); 

~ Step 2: Call a specific method to obtain the desired sensor:

For example, you can obtain a direction sensor and find its own API ~ :

Sensor mSensorOrientation = sm.getDefaultSensor(Sensor.TYPE_ORIENTATION);

~ Step 3: implement the SensorEventListener interface and override the onSensorChanged and onAccuracyChanged methods!

OnSensorChanged: Callback when the sensor value changes
OnAccuracyChanged: Callback when the progress of the sensor changes

@Override    public void onSensorChanged(SensorEvent event) {        final float[] _Data = event.values;       this.mService.onSensorChanged(_Data[0],_Data[1],_Data[2]);    }    @Override    public void onAccuracyChanged(Sensor sensor, int accuracy) {    }

We generally obtain the sensor data from this SensorEvent. This class hasValues variable,
Type isFloat [], This variableThere are only three elements at mostAnd different sensors have different meanings of corresponding elements,
For example, the first element in the direction sensor is the azimuth value, and the first value in the pressure sensor is the pressure value!

~ Step 4: The SensorManager object calls registerListener to register the listener:

ms.registerListener(mContext, mSensorOrientation, android.hardware.SensorManager.SENSOR_DELAY_UI);

The method is also very simple, corresponding parameters:Context object,Sensor Object,
AndPrecision density of sensor Delay TimeThere are four optional values:
SENSOR_DELAY_FASTEST-- Latency:0 ms
SENSOR_DELAY_GAME-- Latency:20 ms
SENSOR_DELAY_UI-- Latency:60 ms
SENSOR_DELAY_NORMAL-- Latency:200 ms
Of course, low latency means more frequent inspection and more power consumption.
Do not use too high precision. Generally, use more than the third ~ Measure it by yourself ~

~ Step 5: cancel the listener registration:

This is a good habit. we can write it to the destroy method of Activity or Service:

    public void destroy(){        ms.unregisterListener(mContext, mSensorOrientation);    }

Okay, the routine is very simple ~

 

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.