Android sensor (details 2)-common Sensors

Source: Internet
Author: User
Tags in degrees

Android sensor (details 2)-common Sensors

 

Direction sensor Orientation (now almost no longer needed, because there is actually no real direction) Magnetic Field Sensor Magnetic Field Temperature sensor Temperature Light pressure sensor Pressure1. direction sensor Orientation:

Purpose:
The orientation sensor is used to detect the placement status of mobile phone devices. For example, the orientation at the top of the mobile phone and the current tilt angle of the mobile phone: with the help of a direction sensor, you can develop a compass and a level. (Note: Now we generally do not need to use a sensor as a compass)

Three Angles returned by the Direction sensor:

First: the angle between the top of the mobile phone and the north. (For example, when the mobile phone rotates around the Z axis, the angle changes.) Second: indicates the angle of the top or bottom of the mobile phone. (for example, when the mobile phone is tilted around the X axis, the angle value changes-180 ~ 180. The value is increased to a negative value on the top of the mobile phone. The third value indicates the angle from the left or right of the mobile phone. The value ranges from 90 to 90 when the mobile phone is tilted around the Y axis ~ 90, left raised to a negative value) 2. Magnetic Field Sensor Magnetic Field: Purpose:
Used to read the magnetic field strength outside the mobile phone device. As the placement status of mobile phone devices changes, the influence of the surrounding magnetic field on the X, Y, and Z directions of mobile phones will change.
The magnetic field sensor returns three values, representing the magnetic field components in the X, Y, and Z directions.

Here we will not introduce it in detail. We will introduce the use of magnetic field sensors together with examples of compass.

3. Temperature sensor Temperature: Purpose:
The temperature sensor is used to obtain the temperature of the mobile phone device.
The temperature sensor returns a data, which indicates the temperature around the mobile phone device, measured in degrees Celsius. 4. Light Sensor: Purpose:
Used to obtain the light intensity of the mobile phone device's environment.
Return a data, representing the intensity of light around the mobile phone device, measured in lux 5. Pressure Sensor Pressure: Purpose:
Used to obtain the pressure of the mobile phone device's environment.
Return a data value, which indicates the pressure around the mobile phone device. Instance:

Function: use several text boxes to display values of different sensors.

Sens?iti=:

Package com. example. myapplication. activitys; import android. hardware. sensor; import android. hardware. sensorEvent; import android. hardware. sensorEventListener; import android. hardware. sensorManager; import android. support. v7.app. appCompatActivity; import android. OS. bundle; import android. view. menu; import android. view. menuItem; import android. widget. textView; import com. example. myapplication. r; import com. example. myapplication. baseactivity. baseActivity; public class sens%ti%extends BaseActivity implements SensorEventListener {// 2. private TextView mTextviewOri; private TextView mTextviewlight; private TextView temperature; private TextView mTextviewtemp; private TextView mTextviewpressure; private SensorManager mSensorManager; @ Override protected void onCreate (Bundle upload) super. onCreate (savedInstanceState); setContentView (R. layout. activity_sensor); mTextviewOri = (TextView) findViewById (R. id. textori); mTextviewlight = (TextView) findViewById (R. id. textlight); mTextviewcichang = (TextView) findViewById (R. id. textcichang); mTextviewtemp = (TextView) findViewById (R. id. texttemper); mTextviewpressure = (TextView) findViewById (R. id. textpressure); // 1. get Sensor Management Service mSensorManager = (SensorManager) getSystemService (SENSOR_SERVICE) ;}@ Override protected void onResume () {super. onResume (); // 3. register the listener mSensorManager for the system's direction sensor. registerListener (this, mSensorManager. getdefasensensor (Sensor. TYPE_ORIENTATION), SensorManager. SENSOR_DELAY_GAME );//. register the mSensorManager listener for the system's magnetic field sensor. registerListener (this, mSensorManager. getdefasensensor (Sensor. TYPE_MAGNETIC_FIELD), SensorManager. SENSOR_DELAY_GAME );//. register the listener mSensorManager for the system temperature sensor. registerListener (this, mSensorManager. getdefasensensor (Sensor. TYPE_AMBIENT_TEMPERATURE), SensorManager. SENSOR_DELAY_GAME );//. register the listener mSensorManager for the system's optical sensor. registerListener (this, mSensorManager. getdefasensensor (Sensor. TYPE_LIGHT), SensorManager. SENSOR_DELAY_GAME );//. register the listener mSensorManager for the system pressure sensor. registerListener (this, mSensorManager. getdefasensensor (Sensor. TYPE_PRESSURE), SensorManager. SENSOR_DELAY_GAME);}/*** the following two methods are not allowed to implement the interface * @ param event */@ Override public void onSensorChanged (SensorEvent event) {// 4. float [] values = event. values; int sensorType = event. sensor. getType (); // obtain the Sensor type of the trigger event StringBuilder sb = null; // determine which Sensor changes the switch (sensorType) {// Sensor case Sensor. TYPE_ORIENTATION: sb = new StringBuilder (); sb. append (angle of rotation around the Z axis :); sb. append (values [0]); sb. append (angle of rotation around the X axis :); sb. append (values [1]); sb. append (angle of rotation around Y axis :); sb. append (values [2]); mTextviewOri. 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 (y direction angle :); sb. append (values [1]); sb. append (z-direction angle :); sb. append (values [2]); mTextviewcichang. setText (sb. toString (); break; // temperature Sensor case Sensor. TYPE_AMBIENT_TEMPERATURE: sb = new StringBuilder (); sb. append (current temperature :); sb. append (values [0]); mTextviewtemp. setText (sb. toString (); break; // optical Sensor case Sensor. TYPE_LIGHT: sb = new StringBuilder (); sb. append (current light intensity :); sb. append (values [0]); mTextviewlight. setText (sb. toString (); break; // pressure Sensor case Sensor. TYPE_PRESSURE: sb = new StringBuilder (); sb. append (current pressure :); sb. append (values [0]); mTextviewpressure. setText (sb. toString (); break ;}@override // callback method when Sensor precision changes public void onAccuracyChanged (sensor Sensor, int accuracy) {}@ Override protected void onPause () {// unregister the sensor listener mSensorManager when the program is paused. unregisterListener (this); super. onPause () ;}@ Override protected void onStop () {// cancel the sensor listener mSensorManager when the program stops. unregisterListener (this); super. onStop ();}}

Layout:


  
       
        
         
       
        
         
          
           
            
             
             
            
           
          
         
        
       
      
     
    
   
  

Effect:

Note:
In fact, the above data is changed during the simulation, but the mobile phone actually has no dynamic location, so I still don't know where the problem is. Hope to know the great god's advice. ^
Also, it can be seen that the program cannot get the values of the sensor and pressure sensor, because my mobile phone does not support reading the temperature sensor and pressure sensor.

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.