<span id="Label3"></p>Android Sensor Development<p><p>In this article we talk about the sensors in android, which is sometimes necessary for us to do some special applications, here I use Xiaomi mobile phone to download an Ann rabbit, we look at the approximate sensor</p></p><p><p></p></p><p><p>Ann bunny, There are a lot of sensors right, but they are basically divided into three categories, in our official document, that is said</p></p><p><p>Sensors_overview</p></p>I. Sensor overview<p><p>Most Android devices have built-in sensors that measure motion, direction, and various environmental conditions. These sensors provide raw data with high accuracy and accuracy, and are useful if you want to monitor the movement or positioning of three-dimensional devices, or if you want to monitor changes in the environment around the DEVICE. For example, a game can track readings from a gravity sensor from a device to infer complex user gestures and movements, such as tilting, shaking, rotating, or Swinging. similarly, weather applications may use the Device's temperature sensor and humidity sensor to calculate and report dew points, or travel applications may use Ground-field sensors and accelerometers to report Compass Orientation.</p></p><p><p>The Android platform supports three main types of sensors:</p></p> <ul> <ul> <li>Motion sensor</li> </ul> </ul><p><p>These sensors measure the acceleration and rotational forces along the three Axes. This category includes accelerometers, gravity sensors, gyroscopes and rotational vector sensors.</p></p> <ul> <ul> <li>Environmental sensors</li> </ul> </ul><p><p>These sensors measure various environmental parameters, such as ambient air temperature and pressure, lighting and Humidity. This category includes barometer, photometer and Thermometer.</p></p> <ul> <ul> <li>Position sensor</li> </ul> </ul><p><p>These sensors measure the physical location of the Device. This category includes directional sensors and magnetometers</p></p><p><p></p></p><p><p>That's The hype, actually, we can summarize this.</p></p> <ul> <ul> <li>Direction type_orientation</li> <li>Accelerated Type_accelerometer</li> <li>Fiber Type_light</li> <li>Magnetic Type_magnetic_field</li> <li>Distance type_proximity</li> <li>Temperature Type_temperature</li> </ul> </ul>TWO. Sensor Frame<p><p>You can use the Android sensor framework to access these sensors and get raw sensor Data. The sensor framework is part of the Android.hardware package, including the following classes and Interfaces:</p></p> <ul> <ul> <li>Sensormanager</li> </ul> </ul><p><p>You can use this class to create an instance of a sensor service. This class provides various methods for accessing and listing sensors, registering and Unregistering sensor event listeners, and obtaining direction Information. The class also provides several sensor constants for reporting sensor accuracy, setting the data acquisition rate, and calibrating the Sensor.</p></p> <ul> <ul> <li>Sensor</li> </ul> </ul><p><p>You can use this class to create an instance of a specific Sensor. This class provides a variety of methods to determine the function of the Sensor.</p></p> <ul> <ul> <li>Sensorevent</li> </ul> </ul><p><p>The system uses this class to create a sensor event object that provides information about the sensor Event. The Sensor event object includes the following information: the original sensor data, the sensor type that generated the event, the accuracy of the data, and the timestamp of the Event.</p></p> <ul> <ul> <li>Sensoreventlistener</li> </ul> </ul><p><p>You can use this interface to create two callback methods that receive notifications when sensor values change or sensor accuracy changes (sensor events).</p></p><p><p>In a typical application, you can use these sensor-related APIs to perform two basic tasks:</p></p> <ul> <ul> <li>Identify sensor and sensor functions</li> </ul> </ul><p><p>If your application has functionality that relies on a specific sensor type or feature, it is useful to recognize the sensor and sensor features at run Time. For example, you might want to identify all the sensors that exist on your device and disable any application functionality that relies on non-existent Sensors. again, you might want to identify all the sensors of a given type, so you can choose the sensor implementation with the best performance.</p></p> <ul> <ul> <li>Monitoring sensor events</li> </ul> </ul><p><p>Monitoring sensor events is how to get raw sensor Data. Sensor events occur whenever the sensor detects changes in its measurement parameters. The sensor event provides you with four messages: the name of the sensor that triggered the event, the timestamp of the event, the accuracy of the event, and the original sensor data that triggered the Event.</p></p>Three. availability of the sensor<p><p>Although sensor availability varies by device, the Android version may vary. This is because Android sensors were introduced during the release of several platforms. For example, Many sensors are introduced in Android 1.5 (api level 3), But some are not implemented until Android 2.3 (api levels 9) can be used. similarly, several sensors were introduced in Android 2.3 (api level 9) and Android 4.0 (API level 14). Two sensors have been deprecated and replaced by newer, better Sensors.</p></p><p><p>Table 2 summarizes the availability of each sensor per Platform. Only four platforms are listed, as these are platforms that involve sensor Changes. Sensors listed as deprecated are still available on subsequent platforms (sensors located on the device), which are compatible with Android's forward compatibility policy.</p></p><p><p></p></p>Four. identify sensor and sensor capabilities<p><p>We try to get the sensor on the device by code, in fact the code is relatively simple, let's take a look at</p></p><pre class="prettyprint"><pre class="prettyprint"><code class=" hljs cs"> <span class="hljs-comment">//获取所有的传感器</span> <span class="hljs-keyword">private</span><span class="hljs-title">getAllSensor</span>() { SensorManager sensorManager = (SensorManager) getSystemService(SENSOR_SERVICE); <span class="hljs-comment">//这里的Type就是我们需要获取的传感器类型,这里我获取全部的</span> List<Sensor> mList = sensorManager.getSensorList(Sensor.TYPE_ALL); <span class="hljs-keyword">return</span> mList; }</code></pre></pre><p><p>We print the next<br></p></p><p><p>In fact, this type is an entity class, which means that we can still get a single element, and i'll print it all Here.</p></p><p><p>In addition to listing the sensors on the device, you can use the common methods of the sensor class to determine the capabilities and properties of the individual Sensors. If you want the application to vary depending on the sensor or sensor functionality available on the DEVICE. For example, you can use the Getresolution () and Getmaximumrange () methods to obtain the resolution and maximum measurement range of the Sensor. You can also use the Getpower () method to obtain the power requirements of the Sensor.</p></p>Five. Light Sensor<p><p>We are here to enumerate a few representative sensors for a moment, through the code of official documents to practice<br>, let's get the rate of the light Sensor.</p></p><pre class="prettyprint"><code class=" hljs java"> <span class="hljs-comment"><span class="hljs-comment">//light sensor</span></span> <span class="hljs-keyword"><span class="hljs-keyword">Private</span></span> <span class="hljs-keyword"><span class="hljs-keyword">void</span></span> <span class="hljs-title"><span class="hljs-title">Lightsensor</span></span>() {sensormanager Sensormanager = (sensormanager) Getsystemservice (sensor_service); Sensor sensor = Sensormanager.getdefaultsensor (sensor.type_light);<span class="hljs-javadoc"><span class="hljs-javadoc">/** * Register Event * CALLBACK * sensor * sampling rate</span> */</span>Sensormanager.registerlistener (<span class="hljs-keyword"><span class="hljs-keyword">New</span></span>Sensoreventlistener () {<span class="hljs-annotation"><span class="hljs-annotation">@Override</span></span> <span class="hljs-keyword"><span class="hljs-keyword"></span> public</span> <span class="hljs-keyword"><span class="hljs-keyword">void</span></span> <span class="hljs-title"><span class="hljs-title">onsensorchanged</span></span>(sensorevent Event) {<span class="hljs-comment"><span class="hljs-comment">//light Intensity</span></span> <span class="hljs-keyword"><span class="hljs-keyword">float</span></span>Values = event.values[<span class="hljs-number"><span class="hljs-number">0</span></span>]; Tvresult.append (values +<span class="hljs-string"><span class="hljs-string">"\ n"</span></span>); }<span class="hljs-annotation"><span class="hljs-annotation">@Override</span></span> <span class="hljs-keyword"><span class="hljs-keyword"></span> public</span> <span class="hljs-keyword"><span class="hljs-keyword">void</span></span> <span class="hljs-title"><span class="hljs-title">onaccuracychanged</span></span>(sensor sensor,<span class="hljs-keyword"><span class="hljs-keyword">int</span></span>Accuracy) {}}, sensor, sensormanager.sensor_delay_ui); }</code></pre><p><p>You can see that I've got the light sensor here, and I've registered a listener, and the rate is in There.</p></p><p><p></p></p><p><p>Generally our auto-adjust brightness this function will need to</p></p><p><p>When using sensors, it is important to note that the recycling of resources</p></p><pre class="prettyprint"><pre class="prettyprint"><code class=" hljs java"> <span class="hljs-annotation">@Override</span> <span class="hljs-keyword">protected</span><span class="hljs-keyword">void</span><span class="hljs-title">onDestroy</span>() { <span class="hljs-keyword">super</span>.onDestroy(); <span class="hljs-keyword">if</span><span class="hljs-keyword">null</span>) { sensorManager.unregisterListener(listener); } }</code></pre></pre><p><p>Here we cancel the monitoring when we destroy it.</p></p><p><p></p></p>Six. Acceleration Sensor<p><p>The acceleration is actually similar to the code of the light sensor, we can look at</p></p><pre class="prettyprint"><pre class="prettyprint"><code class=" hljs cs"> <span class="hljs-comment">//加速度传感器</span> <span class="hljs-keyword">private</span><span class="hljs-keyword">void</span><span class="hljs-title">AccelerometerSensor</span>() { Sensor sensor = sensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER); <span class="hljs-keyword">new</span> LightSensorListener(); sensorManager.registerListener(listener, sensor, sensorManager.SENSOR_DELAY_UI); } </code></pre></pre><p><p>Our listeners</p></p><pre class="prettyprint"><code class=" hljs axapta"><span class="hljs-class"><span class="hljs-class"> <span class="hljs-keyword">class</span> <span class="hljs-title">Lightsensorlistener</span> <span class="hljs-inheritance"><span class="hljs-keyword">implements</span></span> <span class="hljs-title">Sensoreventlistener</span> {</span></span>@Override<span class="hljs-keyword"><span class="hljs-keyword"></span> public</span> <span class="hljs-keyword"><span class="hljs-keyword">void</span></span>Onsensorchanged (sensorevent Event) {<span class="hljs-comment"><span class="hljs-comment">//0:x Axis 1:y axis 2:z</span> shaft</span>float Values0 = event.values[<span class="hljs-number"><span class="hljs-number">0</span></span>]; float values1 = event.values[<span class="hljs-number"><span class="hljs-number">1</span></span>]; float Values2 = event.values[<span class="hljs-number"><span class="hljs-number">2</span></span>];<span class="hljs-comment"><span class="hljs-comment">//calculate Acceleration</span></span>Tvresult.append (<span class="hljs-string"><span class="hljs-string">"x axis:"</span></span>+values0 +<span class="hljs-string"><span class="hljs-string">"y-axis:"</span></span>+ Values1 +<span class="hljs-string"><span class="hljs-string">"z axis:"</span></span>+ Values2 +<span class="hljs-string"><span class="hljs-string">"\ n"</span></span>); } @Override<span class="hljs-keyword"><span class="hljs-keyword"></span> public</span> <span class="hljs-keyword"><span class="hljs-keyword">void</span></span>Onaccuracychanged (sensor sensor,<span class="hljs-keyword"><span class="hljs-keyword">int</span></span>Accuracy) {}}</code></pre><p><p>So that we can detect the</p></p><p><p></p></p><p><p>From the source can be seen</p></p><p><p></p></p><p><p>We can use Newton's Second law to calculate the values we want based on these three values, or apply our own formulas to manipulate them.</p></p>Seven. Direction Sensor<p><p>Write three sensors, in fact, we have a different way of writing, if you do not know the specific use of values, you can directly open the values of the source to see his comments, to find the corresponding sensor is good, we come to see the next direction, and so on to try to achieve a compass</p></p><p><p>In the same way, we implement the code first</p></p><pre class="prettyprint"><pre class="prettyprint"><code class=" hljs cs"><span class="hljs-comment">//方向传感器</span> <span class="hljs-keyword">private</span><span class="hljs-keyword">void</span><span class="hljs-title">OrientationSensor</span>() { Sensor sensor = sensorManager.getDefaultSensor(Sensor.TYPE_ORIENTATION); <span class="hljs-keyword">new</span> LightSensorListener(); sensorManager.registerListener(listener, sensor, sensorManager.SENSOR_DELAY_FASTEST); }</code></pre></pre><p><p>And the function of our compass is also monitored through this</p></p><pre class="prettyprint"><pre class="prettyprint"><code class=" hljs axapta"><span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">LightSensorListener</span> <span class="hljs-inheritance"><span class="hljs-keyword">implements</span></span> <span class="hljs-title">SensorEventListener</span> {</span> @Override <span class="hljs-keyword">public</span><span class="hljs-keyword">void</span> onSensorChanged(SensorEvent event) { <span class="hljs-comment">//夹角度数</span> float values = event.values[<span class="hljs-number">0</span>]; } @Override <span class="hljs-keyword">public</span><span class="hljs-keyword">void</span><span class="hljs-keyword">int</span> accuracy) { } }</code></pre></pre>Eight. Compass<p><p>compass, we can do it First. UI implementation</p></p><pre class="prettyprint"><code class=" hljs xml"><span class="hljs-tag"><span class="hljs-tag"><<span class="hljs-title">relativelayout</span><span class="hljs-attribute">android:layout_width</span>=<span class="hljs-value">"200dp"</span><span class="hljs-attribute">android:layout_height</span>= <span class="hljs-value">"200dp"</span>> </span></span> <span class="hljs-tag"><span class="hljs-tag"><<span class="hljs-title">ImageView</span><span class="hljs-attribute">android:id</span>= "<span class="hljs-value">@+id/iv_point"</span><span class="hljs-attribute">android:layout_width</span>=<span class="hljs-value">" Wrap_content "</span><span class="hljs-attribute">android:layout_height</span>=<span class="hljs-value">" Wrap_content "</span><span class="hljs-attribute">android:layout_ Centerinparent</span>=<span class="hljs-value">"true"</span><span class="hljs-attribute">android:src</span>=<span class="hljs-value">"@drawable/iv_pointer"</span>/> </span></span> <span class="hljs-tag"><span class="hljs-tag"><<span class="hljs-title">textview </span> <span class="hljs-attribute">android:layout _width </span> =<span class="hljs-value"> "wrap_content" </span> <span class="hljs-attribute">android:layout_height </span> =<span class="hljs-value"> "wrap_content" </span> <span class="hljs-attribute">android:layout_centerhorizo Ntal </span> =<span class="hljs-value"> "true" </span> <span class="hljs-attribute">android:text </span> =<span class="hljs-value"> "north" </span> <span class="hljs-attribute">android:textcolor </span> =<span class="hljs-value"> "@andro Id:color/background_dark "</span> <span class=" hljs-attribute ">android:textsize </span> =<span class=" hljs-value "> "18sp" </span>/> </span></span> <span class="hljs-tag"><span class="hljs-tag"><<span class="hljs-title">textview </span> <span class="hljs-attribute">android:layout _width </span> =<span class="hljs-value"> "wrap_content" </span> <span class="hljs-attribute">android:layout_height </span> =<span class="hljs-value"> "wrap_content" </span> <span class="hljs-attribute">ANDROID:LAYOUT_ALIGNPARENTB Ottom </span> =<span class="hljs-value"> "true" </span> <span class="hljs-attribute">android:layout_centerhorizonta L </span> =<span class="hljs-value" "true" < span> <span class="hljs-attribute">android:text </span> =<span class="h Ljs-value ">" South "</span> <span class=" hljs-attribute ">android:textcolor </span> =<span class=" hljs-value ">" @android: Color/background_dark "</span> <span class=" hljs-attribute ">android:textsize </span> =<span class=" hljs-value ">" 18 SP "</span>/> </span></span></span> <span class="hljs-tag"><span class="hljs-tag"><<span class="hljs-title">textview </span> <span class="hljs-attribute">android:layout _width </span> =<span class="hljs-value"> "wrap_content" </span> <span class="hljs-attribute">android:layout_height </span> =<span class="hljs-value"> "wrap_content" </span> <span class="hljs-attribute">android:layout_centervertic Al </span> =<span class="hljs-value"> "true" </span> <span class="hljs-attribute">android:text </span> =<span class=" Hljs-value ">" West "</span> <span class=" hljs-attribute ">android:textcolor </span> =<span class=" hljs-value ">" @android : Color/background_dark "</span> <span class=" hljs-attribute ">android:textsize </span> =<span class=" hljs-value ">" 1 8SP "</span>/> </span></span> <span class="hljs-tag"><span class="hljs-tag"><<span class="hljs-title">TextView</span><span class="hljs-attribute">android:layout_width</span>=<span class="hljs-value">"wrap_content"</span><span class="hljs-attribute">android:layout_height</span>= <span class="hljs-value">"wrap_content"</span> <span class="hljs-attribute">android:layout_alignparentright</span>=<span class="hljs-value">"true"</span><span class="hljs-attribute">android:layout_centervertical</span>=<span class="hljs-value">"true"</span> <span class="hljs-attribute">android:text</span>=<span class="hljs-value">"east"</span><span class="hljs-attribute">android:textcolor</span>=<span class="hljs-value">"@android: color/background_dark"</span> <span class="hljs-attribute">android:textsize</span>=<span class="hljs-value">"18sp"</span>/> </span></span> <span class="hljs-tag"><span class="hljs-tag"></<span class="hljs-title">relativelayout</span>></span></span></code></pre><p><p>The effect of the implementation</p></p><p><p></p></p><p><p>The main way we do this is to animate this pointer, where a spinning animation is used directly</p></p><pre class="prettyprint"><code class=" hljs java">Class Lightsensorlistener implements Sensoreventlistener {<span class="hljs-keyword"><span class="hljs-keyword">float</span></span>Lastvalues =<span class="hljs-number"><span class="hljs-number">0</span></span>;<span class="hljs-annotation"><span class="hljs-annotation">@Override</span></span> <span class="hljs-keyword"><span class="hljs-keyword"></span> public</span> <span class="hljs-keyword"><span class="hljs-keyword">void</span></span> <span class="hljs-title"><span class="hljs-title">onsensorchanged</span></span>(sensorevent Event) {<span class="hljs-comment"><span class="hljs-comment">//clamp Angle Number</span></span> <span class="hljs-keyword"><span class="hljs-keyword">float</span></span>Values = event.values[<span class="hljs-number"><span class="hljs-number">0</span></span>];<span class="hljs-comment"><span class="hljs-comment">//tvresult.settext ("angle:" + values);</span></span> <span class="hljs-javadoc"><span class="hljs-javadoc">/** * 0: East * 90: South * 180: West * 270: North</span> * *</span>Rotateanimation Anim =<span class="hljs-keyword"><span class="hljs-keyword">New</span></span>Rotateanimation (-lastvalues, values, animation.relative_to_self,<span class="hljs-number"><span class="hljs-number">0.5</span></span>f, animation.relative_to_self,<span class="hljs-number"><span class="hljs-number">0.5</span></span>f); Iv_point.startanimation (anim); Lastvalues = values; }<span class="hljs-annotation"><span class="hljs-annotation">@Override</span></span> <span class="hljs-keyword"><span class="hljs-keyword"></span> public</span> <span class="hljs-keyword"><span class="hljs-keyword">void</span></span> <span class="hljs-title"><span class="hljs-title">onaccuracychanged</span></span>(sensor sensor,<span class="hljs-keyword"><span class="hljs-keyword">int</span></span>Accuracy) {}</code></pre><p><p>So we can make it Happen.</p></p><p><p></p></p><p><p>ok, here, Our sensors on the BB finished, interested students can add group</p></p>Android Travel by Road: 484167109<p><p>of course, you can also follow my public number.</p></p><p><p></p></p>Sample Download: http://download.csdn.net/detail/qq_26787115/9829628<p><p>Android Sensor Development</p></p></span>
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