Android --- 57 --- sensor, android Sensor

Source: Internet
Author: User

Android --- 57 --- sensor, android Sensor

The Android system provides support for sensors.
Developing an application sensor is simple. You only need to register a listener for the specified listener.


Steps:
1. Call the getSystemService (Context. SENSOR_SERVICE) method of Context to obtain the SensorManager object. The SensorManager object represents the system's sensor management service.
2. Call the getdefasensensor (int type) method of SensorManager to obtain the sensor of the specified type.
3. Generally, you can call the registerListener () of SensorManager in the onResume () method of the Activity to register the listener for the specified sensor.
The program can obtain the data transmitted from the sensor by implementing the listener.

The Sensor registration method provided by SensorManager is: registerListener (SensorEventListener listener, sensor Sensor, int rate ):
Parameters:
Listener:
Listen to the sensor listener. The listener must implement the SensorEventListener interface.
Sensor: sensing object
Rate: the frequency at which the sensor data is obtained.
The frequency is as follows:

SensorManager. SENSOR_DELAY_FASTEST:
The fastest, with the lowest latency. This frequency is recommended only for applications that are particularly dependent on sensor data.

SensorManager. SENSOR_DELAY_GAME:
Suitable for games. This frequency is suitable for general practical applications.

SensorManager. SENSOR_DELAY_NORMAL:
Normal Frequency. Generally, applications with low real-time requirements are suitable for use.

SensorManager. SENSOR_DELAY_UI:
Suitable for the frequency of common user interfaces. It is used in common applets.

 

 

Commonly used Android sensors:

Direction, magnetic field, temperature, light, pressure.

 

 

 

Layout file:


 

<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:orientation="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="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/temperature" />    <EditText        android:id="@+id/etTemerature"        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/light" />    <EditText        android:id="@+id/etLight"        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/pressure" />    <EditText        android:id="@+id/etPressure"        android:layout_width="fill_parent"        android:layout_height="wrap_content"        android:cursorVisible="false"        android:editable="false" /></LinearLayout>


 

Activity:

 

Public class MainActivity extends Activity implements SensorEventListener {// defines SensorManagerSensorManager mSensorManager; // EditText etOrientation; // magnetic field EditText etMagnetic; // temperature EditText etTemerature; // optical 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 (); // registers the listener mSensorManager for the sensor. registerListener (this, mSensorManager. getdefasensensor (Sensor. TYPE_ORIENTATION), SensorManager. SENSOR_DELAY_GAME); // registers the listener mSensorManager for the system's magnetic field sensor. registerListener (this, mSensorManager. getdefasensensor (Sensor. TYPE_MAGNETIC_FIELD), SensorManager. SENSOR_DELAY_GAME); // registers the listener mSensorManager for the system temperature sensor. registerListener (this, mSensorManager. getdefasensensor (Sensor. TYPE_AMBIENT_TEMPERATURE), SensorManager. SENSOR_DELAY_GAME); // registers the listener mSensorManager for the system's optical sensor. registerListener (this, mSensorManager. getdefasensensor (Sensor. TYPE_LIGHT), SensorManager. SENSOR_DELAY_GAME); // registers the listener mSensorManager for the system pressure sensor. registerListener (this, mSensorManager. getdefasensensor (Sensor. TYPE_PRESSURE), SensorManager. SENSOR_DELAY_GAME) ;}@ Overrideprotected void onStop () {mSensorManager. unregisterListener (this); super. onStop () ;}@ Overrideprotected void onPause () {mSensorManager. unregisterListener (this); super. onPause () ;}// the following method is required to implement the SensorEventListener interface @ 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 ("angle of rotation around the Z axis:"); sb. append (values [0]); sb. append ("\ n angle around the X axis:"); sb. append (values [1]); sb. append ("\ n rotation angle 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 ("X direction angle:"); sb. append (values [0]); sb. append ("\ nY direction angle:"); sb. append (values [1]); sb. append ("\ nZ angle:"); 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; // optical Sensor case Sensor. TYPE_LIGHT: sb = new StringBuilder (); sb. append ("current light intensity:"); 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;} // this method is called back when the Sensor precision changes. @ Overridepublic void onAccuracyChanged (sensor Sensor, int accuracy) {// TODO Auto-generated method stub }}


 

 

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.