Android------sensor

Source: Internet
Author: User
<span id="Label3"></p><p><p>The Android system provides support for the Sensor.<br>Developing an application sensor is simple, just registering a listener for the specified listener.</p></p><p><p><br>Steps:<br>1. Call the Getsystemservice (context.sensor_service) method of the Context to get the Sensormanager object, and the Sensormanager object represents the System's sensor management Service<br>2. Call Sensormanager's getdefaultsensor (int Type) method to get the sensor of the specified type<br>3. Usually choose to call Sensormanager's Registerlistener () in the Activity's onresume () method to register the listener for the specified Sensor.<br>The program can get the data returned by the sensor by implementing the Listener.</p></p><p><p>Sensormanager provides a method for registering sensors: registerlistener (sensoreventlistener listener,sensor sensor, int rate):<br>Parameters:<br>Listener<br>Monitor the Sensor's listener. The listener needs to implement the Sensoreventlistener interface<br>Sensor: Sensor Object<br>rate: specifies how often to get sensor Data.<br>There are several frequencies:</p></p><p><p>Sensormanager.sensor_delay_fastest:<br>The fastest, least latency, is recommended for applications that rely specifically on sensor Data.</p></p><p><p>Sensormanager.sensor_delay_game:<br>The frequency for the Game. This frequency is suitable for use in general practical applications.</p></p><p><p>Sensormanager.sensor_delay_normal:<br>Normal Frequency. General real-time requirements are not particularly high for applications that are suitable for use.</p></p><p><p>Sensormanager.sensor_delay_ui:<br>The frequency for the normal user interface. Used in ordinary small programs.</p></p><p><p></p></p><p><p></p></p><p><p>Commonly used sensors for android:</p></p><p><p>direction, Magnetic field, temperature, light, pressure.<br></p></p><p><p></p></p><p><p></p></p><p><p></p></p><p><p>Layout file:</p></p><p><p><br></p></p><pre class="html" name="code"><linearlayout xmlns:android= "http://schemas.android.com/apk/res/android" xmlns:tools= "http// Schemas.android.com/tools "android:layout_width=" fill_parent "android:layout_height=" fill_parent "android:orienta tion= "vertical" android:paddingbottom= "@dimen/activity_vertical_margin" android:paddingleft= "@dimen/activity_ Horizontal_margin "android:paddingright=" @dimen/activity_horizontal_margin "android:paddingtop=" @dimen/activity_ Vertical_margin "tools:context=" com.example.sensor.MainActivity "> <textview android:layout_width=" Fill _parent "android:layout_height=" wrap_content "android:text=" @string/orientation "/> <edittext android:id= "@+id/etorientation" android:layout_width= "fill_parent" android:layout_height= "wrap_content" android:cursorvisible= "false" android:editable= "false"/> <textview android:layout_width= "fill _parent "android:layout_height=" wrap_content " android:text= "@string/magnetic"/> <edittext android:id= "@+id/etmagnetic" android:layout_width= "f Ill_parent "android:layout_height=" wrap_content "android:cursorvisible=" false "android:editable=" Fals E "/> <textview android:layout_width=" fill_parent "android:layout_height=" wrap_content "android oid:text= "@string/temperature"/> <edittext android:id= "@+id/ettemerature" android:layout_width= "fi Ll_parent "android:layout_height=" wrap_content "android:cursorvisible=" false "android:editable=" False "/> <textview android:layout_width=" fill_parent "android:layout_height=" wrap_content "andro id:text= "@string/light"/> <edittext android:id= "@+id/etlight" android:layout_width= "fill_parent" android:layout_height= "wrap_content" android:cursorvisible= "false" android:editable= "false"/> & Lt TextView AndroId:layout_width= "fill_parent" android:layout_height= "wrap_content" android:text= "@string/pressure"/> <edittext android:id= "@+id/etpressure" android:layout_width= "fill_parent" android:layout_height= "w Rap_content "android:cursorvisible=" false "android:editable=" false "/></linearlayout></pre><p><p><br></p></p><p><p>Activity:</p></p><p><p></p></p><pre class="html" name="code">public class Mainactivity extends Activity implements Sensoreventlistener {//definition Sensormanagersensormanager Msensormanager;//direction edittext etorientation;//magnetic field edittext etmagnetic;//temperature edittext ettemerature;//light edittext etlight;// Pressure edittext etpressure; @Overrideprotected void onCreate (Bundle Savedinstancestate) {super.oncreate ( savedinstancestate); setcontentview (r.layout.activity_main); etorientation = (EditText) Findviewbyid ( r.id.etorientation); etmagnetic = (EditText) Findviewbyid (r.id.etmagnetic); ettemerature = (EditText) Findviewbyid ( r.id.ettemerature); etlight = (EditText) Findviewbyid (r.id.etlight); etpressure = (EditText) Findviewbyid ( R.id.etpressure);//get Sensor Management Service Msensormanager = (sensormanager) Getsystemservice (sensor_service);} @Overrideprotected void Onresume () {super.onresume ();//register listener Msensormanager.registerlistener for direction sensor (this, Msensormanager.getdefaultsensor (sensor.type_orientation), sensormanager.sensor_delay_game);// Register the Listener Msensormanager.registerlistener for the System's magnetic field sensor (THIS,MSENSORMANAGER.GEtdefaultsensor (sensor.type_magnetic_field), sensormanager.sensor_delay_game);// Register the listener for the System's temperature sensor Msensormanager.registerlistener (this, Msensormanager.getdefaultsensor (sensor.type_ambient_ temperature), sensormanager.sensor_delay_game);//for The System's optical sensor Register listener Msensormanager.registerlistener (this, Msensormanager.getdefaultsensor (sensor.type_light), sensormanager.sensor_delay_game);// Register the Listener Msensormanager.registerlistener (this,msensormanager.getdefaultsensor (sensor.type_pressure) for the system's pressure sensor, sensormanager.sensor_delay_game);} @Overrideprotected void OnStop () {msensormanager.unregisterlistener (this); super.onstop ();} @Overrideprotected void OnPause () {msensormanager.unregisterlistener (this); super.onpause ();} The following are the methods that implement the Sensoreventlistener interface that must be implemented @overridepublic void onsensorchanged (sensorevent event) {float values[] = Event.values;int SensorType = Event.sensor.getType (); StringBuilder sb = new StringBuilder (); switch (sensortype) {//direction sensor case SENSOR.TYPE_ORIENTATION:SB = new StringBuilder () Sb.append ("the angle of rotation around the z axis:"); sB.append (values[0]); sb.append ("the angle of the x-axis turned:"), sb.append (values[1]), sb.append ("the angle that goes around the y axis:"); sb.append (values[2) ); etorientation.settext (sb.tostring ()); break;//magnetic field sensor Case SENSOR.TYPE_MAGNETIC_FIELD:SB = new StringBuilder (); Sb.append ("angle in x direction:"), sb.append (values[0]), sb.append ("angle in \ny direction:"), sb.append (values[1]), sb.append ("angle in \nz direction:" ); sb.append (values[2]); etmagnetic.settext (sb.tostring ()); break;//temperature sensor Case SENSOR.TYPE_AMBIENT_TEMPERATURE:SB = New StringBuilder (); sb.append ("current temperature:"); sb.append (values[0]); ettemerature.settext (sb.tostring ()); break;// Light sensor Case SENSOR.TYPE_LIGHT:SB = new StringBuilder () sb.append ("the intensity of the current light is:"); sb.append (values[0]); Etlight.settext ( Sb.tostring ()); break;//pressure sensor Case SENSOR.TYPE_PRESSURE:SB = new StringBuilder () sb.append ("current pressure:"); sb.append ( values[0]); etpressure.settext (sb.tostring ()); break;default:break;}} Callback this method when the sensor accuracy changes @overridepublic void onaccuracychanged (sensor sensor, int Accuracy) {//TODO auto-generated method stub} }</pre><p><p><br></p></p><p><p></p></p><p><p>Android------sensor</p></p></span>

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.