Android development-Summary of commonly used sensors

Source: Internet
Author: User

Android development-Summary of commonly used sensors

With the development of mobile phones, there are more and more types of sensors supported by various mobile phones. Using sensors in development makes people feel refreshed, for example, shaking, and shake and cut songs in the mobile music player. Today, we will briefly introduce the use of sensors and some commonly used sensors in Android.

I. Use of Sensors

1. First, we need to obtain the sensor management object through mSensorManager = (SensorManager) getSystemService (Context. SENSOR_SERVICE );

2. add listening events to the SensorManager object. You can use the registerListener method of SensorManage to add listening events to our sensors. This method has multiple overload forms, generally, we need to provide a listener object and implement the SensorEventListener interface, then the Sensor type (int type value), or a sensor Sensor Object (a sensor object can be obtained through getdefasensensor ).

There are two important methods in the SensorEventListener interface,

1) public void onAccuracyChanged (Sensor sensor, int degree), called when the accuracy of the Sensor changes, degree is the new sensor precision.

2) public void onSensorChanged (SensorEvent event): This method is executed when the sensor information changes. The values member in the event object contains the important information of the sensor we need, this is a float array value. Generally, the value varies depending on the sensor type. Next I will introduce the values meanings of some common sensor types. In this method, we usually process our own tasks based on the obtained sensor information.

3. When we do not need to listen to sensors, we use an unregisterListener method to destroy registration.

II. Introduction to common sensor types

As we have known before, values in SensorEvent is an important variable for obtaining sensor information. Next we will introduce the values meanings of some common sensors.

Take the X axis along the phone screen, and the right to the right. Take the Y axis along the phone screen, and the right to the top. The vertical line is the Z axis and the right to the outside.

1. Sensor. TYPE_ACCELEROMETER: Acceleration Sensor

Values [0]: acceleration of gravity in the X axis.

Values [1]: acceleration of gravity in the Y axis.

Values [2]: acceleration of gravity in the Z axis.

2. Sensor. TYPE_GYROSCOPE: gyroscope Sensor

Values [0]: The angular velocity that rotates along the X axis.

Values [1]: The angular velocity that rotates along the Y axis.

Values [2]: returns the rotation speed along the Z axis.

3. Sensor. TYPE_ORIENTATION: Direction Sensor

The rotation vector represents the combination of the orientation angle of the device and the axis of the device through a angle θ to rotate around the axis <x, y, z>.

Values [0]: the angle from which the mobile phone rotates around the Z axis. 0 indicates North (North); 90 indicates East (East); 180 indicates South (South); 270 indicates West (West ). If the phone is placed horizontally, it indicates that the angle between the front of the phone and the North is the value.

Values [1]: The angle from which the phone rotates around the X axis. The value range is-180 to + 180 degrees.

Values [2]: The angle from which the phone rotates around the Y axis. The value range is-90 to + 90 degrees.

4. Sensor. TYPE_LIGHT: Light Sensor

Values [0]: environmental light horizontal Lux unit value.

5. Sensor. TYPE_PRESSURE: Pressure Sensor

Values [0]: atmospheric pressure value, unit: Pa.

6. Sensor. TYPE_PROXIMITY: short-range Sensor

Values [0]: The distance value in centimeters.

7. Sensor. TYPE_RELATIVE_HUMIDITY: Humidity Sensor

Values [0]: relative humidity percentage of surrounding air.

8. Sensor. TYPE_TEMPERATURE: Temperature Sensor

Values [0]: The degree Celsius value of the ambient temperature.

Android APIs define more than a dozen sensor types. Each mobile phone cannot support all sensor types. If the returned value of the getdefasensensor method is null, the mobile phone does not support this type of sensor, when we register a monitoring event for an unsupported sensor, no exception is thrown, but no valid data can be obtained.

3. Implement the shake function by using the Acceleration Sensor

Next, we use the code to simulate a shake function, mainly through the acceleration sensor. When an event is triggered, we call the mobile phone vibrator and display it with Toast. When calling the mobile phone vibration prompt, you need to add permissions for our app.

 
Next is our code section
Package com. example. shaketest; import android. app. activity; import android. content. context; import android. hardware. sensor; import android. hardware. sensorEvent; import android. hardware. sensorEventListener; import android. hardware. sensorManager; import android. OS. bundle; import android. OS. vibrator; import android. util. log; import android. view. menu; import android. widget. toast; public class MainActivity extends Activity {private SensorManager mSensorManager; // sensor management class private Vibrator mVibrator; // Vibrator private static final String TAG = "MainActivity"; @ Overrideprotected void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); setContentView (R. layout. activity_main); mSensorManager = (SensorManager) getSystemService (Context. SENSOR_SERVICE); mVibrator = (Vibrator) getSystemService (Context. VIBRA TOR_SERVICE); // get the mobile phone vibrator} @ Overrideprotected void onPause () {// TODO Auto-generated method stubsuper. onPause (); if (mSensorManager! = Null) {mSensorManager. unregisterListener (mySensorListener) ;}}@ Overrideprotected void onResume () {// TODO Auto-generated method stubsuper. onResume (); if (mSensorManager! = Null) {// The first parameter is Listener, the second parameter is the sensor type, and the third parameter value obtains the Sensor Information Frequency mSensorManager. registerListener (mySensorListener, mSensorManager. getdefasensensor (Sensor. TYPE_ACCELEROMETER), SensorManager. SENSOR_DELAY_NORMAL); // SensorManager. SENSOR_DELAY_NORMAL: the default speed for obtaining sensor data. // SensorManager. SENSOR_DELAY_GAME: this value is recommended if a game is developed using sensors. // SensorManager. SENSOR_DELAY_UI: this value is recommended if you use a sensor to update data in the UI .}} Private SensorEventListener mySensorListener = new SensorEventListener () {@ Overridepublic void onAccuracyChanged (Sensor sensor, int degree) {// TODO Auto-generated method stub // called when the sensor's accuracy changes, int Is the new sensor's precision} @ Overridepublic void onSensorChanged (SensorEvent event) {// TODO Auto-generated method stub // execute this method float [] values = event when the sensor information changes. values; // This variable has a maximum of three elements. The elements in the values variable have different meanings depending on the sensor. Float x = values [0]; // acceleration of gravity in the x axis float y = values [1]; // acceleration of gravity in the y axis float z = values [2]; // gravity acceleration in the Z axis // set the gravity acceleration value. When the shaking reaches this value, the system triggers the vibration and corresponding event int medumValue = 19; if (Math. abs (x)> medumValue) {mVibrator. vibrate (500); Log. w (TAG, "x_shake"); Log. w (TAG, "x axis:" + x + "; y axis:" + y + "; z axis:" + z); Toast. makeText (MainActivity. this, "Shaking !!!! ", Toast. LENGTH_SHORT ). show ();} if (Math. abs (y)> medumValue) {mVibrator. vibrate (500); Log. w (TAG, "y_shake"); Log. w (TAG, "x axis:" + x + "; y axis:" + y + "; z axis:" + z); Toast. makeText (MainActivity. this, "Shaking !!!! ", Toast. LENGTH_SHORT ). show ();} if (Math. abs (z)> medumValue) {mVibrator. vibrate (500); Log. w (TAG, "z_shake"); Log. w (TAG, "x axis:" + x + "; y axis:" + y + "; z axis:" + z); Toast. makeText (MainActivity. this, "Shaking !!!! ", Toast. LENGTH_SHORT ). show () ;}};@ Overridepublic boolean onCreateOptionsMenu (Menu menu) {// Inflate the menu; this adds items to the action bar if it is present. getMenuInflater (). inflate (R. menu. main, menu); return true ;}}



Related Article

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.