Android Basics Getting Started Tutorial--10.10 sensor Special (1)--related introduction
tags (space delimited): Android Basics Getting Started Tutorial
1. Introduction of Sensors:
When it comes to sensors, I believe we will not be strangers, such as the shaking of the use of the acceleration sensor;
the definition of a sensor : a physical device or biological organ capable of detecting and sensing external signals, physical conditions (such as light, heat,
or chemical composition (such as smog), and pass the information of discovery to other devices or organs!
sensor type : The sensor can be divided from different angles, the principle of conversion (the basic physical or chemical sensor work
effects), use, output signals and production materials and processes. Generally according to the work of the original points: physical sensors and chemical sensors
Two categories! Mobile phones are basically equipped with physical sensors, and the sensors on the phone have the following:
- Direction sensors (Orientation sensor)
- Acceleration Sensor (Accelerometer sensor)
- Gyroscope Sensors (Gyroscope sensor)
- magnetic field sensors (magnetic field sensor)
- Distance sensors (Proximity sensor)
- Light sensor
- Barometric pressure sensors (pressure sensor)
- Temperature sensors (temperature sensor)
- Gravity sensor (introduced with Gravity sensor,android 2.3)
- Linear acceleration Sensor (Linear acceleration sensor, Android 2.3 introduced)
- Rotational vector sensor (Rotation vector sensor,android 2.3)
- Relative humidity sensor (Relative Humidity sensor,android 4.0)
- Near Field communication (NFC) sensors (introduced by Android 2.3), NFC is not the same as other, with read and write capabilities.
Of course, besides these, there are other things like heart rate sensors, step sensors, fingerprint sensors,
About the sensor types supported by Android devices Official documentation: Sensor summary part of the content ~
2. How do I see which sensors are supported by my phone?
So many of these are definitely not available on every phone, and the type of sensors that are on each phone
And the number may be different, for example, the Nexus 5 I have on hand supports sensor types:
Gravity, light, distance, barometric pressure and gyroscope! And the accidental Moto X second generation has gravity, light,
Distance and infrared sensors! About the types of sensors supported by your phone, you can go to relevant reviews
Website such as: Zhongguancun mobile phone online, Pacific, etc., search their own models to view the relevant parameters!
Of course, we can also write our own code to see the sensor type supported by our phone ~
code example :
Run :
Code Implementation :
activity_main.xml:
<relativelayout xmlns:android="Http://schemas.android.com/apk/res/android" android:layout_width="Match_parent"android:layout_height="Match_parent" > <ScrollViewandroid:layout_width="Match_parent"android:layout_height ="Match_parent"> <TextViewandroid:id= "@+id/txt_show"android:layout_width=" Match_parent "android:layout_height=" match_parent " /> </ScrollView></relativelayout>
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<sensor> allsensors = SM. Getsensorlist(Sensor. TYPE_all);StringBuilder sb = new StringBuilder ();Sb. Append("This phone has"+ allsensors. Size() +"a sensor, respectively: \ n");for (Sensor s:allsensors) {switch (s. GetType()) {case Sensor. TYPE_accelerometer:sb. Append(s. GetType() +"Accelerometer (Accelerometer sensor)"+"\ n"); Break;Case Sensor. TYPE_gyroscope:sb. Append(s. GetType() +"Gyro sensor (Gyroscope sensors)"+"\ n"); Break;Case Sensor. TYPE_light:sb. Append(s. GetType() +"Light sensor"+"\ n"); Break;Case Sensor. TYPE_magnetic_field:sb. Append(s. GetType() +"magnetic field sensors (magnetic field sensor)"+"\ n"); Break;Case Sensor. TYPE_orientation:sb. Append(s. GetType() +"Direction sensor (Orientation sensors)"+"\ n"); Break;Case Sensor. TYPE_pressure:sb. Append(s. GetType() +"Barometric pressure sensor (pressure)"+"\ n"); Break;Case Sensor. TYPE_proximity:sb. Append(s. GetType() +"Distance sensor (Proximity)"+"\ n"); Break;Case Sensor. TYPE_temperature:sb. Append(s. GetType() +"Temperature Sensors (temperature sensor)"+"\ n"); Break;Default:sb. Append(s. GetType() +"Other Sensors"+"\ n"); Break;} SB. Append("Device Name:"+ S. GetName() +"\ n Device version:"+ S. GetVersion() +"\ n Supplier:"+ S. Getvendor() +"\ n");} txt_show. SetText(SB. toString());}}
3.Sensor sensor-related methods and use routines
From the example in 2 we can summarize some information about acquiring sensor sensors and getting sensors.
The process is as follows:
1) sensor-related methods
Step 1: Get the Sensor manager :
SensorManager sm = (SensorManager)getSystemService(SENSOR_SERVICE);
- Step 2: Get a list of sensor objects for the device :
List<Sensor> allSensors = sm.getSensorList(Sensor.TYPE_ALL);
- Step 3: Iterate to get the sensor object, and then call the corresponding method to obtain information about the sensors:
for(Sensor s:allSensors){ sensor.getName(); //获得传感器名称 sensor.getType(); //获得传感器种类 sensor.getVendor(); //获得传感器供应商 sensor.getVersion(); //获得传感器版本 sensor.getResolution(); //获得精度值 //获得最大范围 sensor.getPower(); //传感器使用时的耗电量 }
2) Sensor usage routines
In general we rarely say go directly to get sensor, then get the above information! Because it's nothing.
More often than not, we are going to get the data that the sensor collects, like getting the current atmospheric pressure,
Or the value of the three corners of the direction sensor, or the value of the gyroscope, and most of the sensor data is
One of the following routines:
~step 1: Get the Sensor manager:
SensorManager sm = (SensorManager)getSystemService(SENSOR_SERVICE);
~step 2: Call a specific method to obtain the desired sensor:
For example, here is the direction of the sensor, want to get what sensor self-check api~:
Sensor mSensorOrientation = sm.getDefaultSensor(Sensor.TYPE_ORIENTATION);
~step 3: Implement Sensoreventlistener interface, rewrite onsensorchanged and onaccuracychanged Method!
onsensorchanged: Callback When the value of the sensor changes
onaccuracychanged: Callback when the sensor's progress has changed
@Override Span class= "Hljs-keyword" >public void onsensorchanged (Sensorevent event) {final flo At [] _data = event.values; this . mservice.onsensorchanged (_data[0 ],_ Data[1 ],_data[2 ]); } @Override public void onaccuracychanged (sensor sensor, int accuracy) {}
The source of the sensor data we generally get is this sensorevent, which has a variable of values ,
Type is float[], the variable has a maximum of only three elements , and the sensor is different, the corresponding element represents a different meaning,
For example, the first element in the direction sensor is the azimuth value, and the first value in the barometric pressure sensor is the barometric value!
~step 4:sensormanager Object calls Registerlistener Register listener:
ms.registerListener(mContext, mSensorOrientation, android.hardware.SensorManager.SENSOR_DELAY_UI);
The method is also very simple, corresponding to the parameters: Context object ,sensor object ,
And the accuracy density of the sensor's delay time , there are four selectable values:
sensor_delay_fastest--delay:0ms
sensor_delay_game--delay:20ms
sensor_delay_ui--delay:60ms
sensor_delay_normal--delay:200ms
Of course, low latency means more frequent inspection of cars, which means more power consumption, if not very high precision recommendations
Do not use too high precision, generally with a third more ~ their own measure it ~
~step 5: Cancellation of listener registration:
When you're done, it's a good habit to write him down to the activity or service destruction method:
publicvoiddestroy(){ ms.unregisterListener(mContext, mSensorOrientation); }
OK, the routine is very simple ~
4. The sample code for this section is downloaded:
Sensordemo1.zip
This section summarizes:
OK, this section gives you an introduction to the sensors in Android and how to understand the sensors that your phone supports,
In addition to online search, you can also write code test, and then also explained the sensor sensors related information acquisition method flow, and finally
Also explained the collection of sensor data routines, the following we will be for some common use of sensors to analyze, please look forward to ~
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
Android Basics Getting Started Tutorial--10.10 sensor Special (1)--related introduction