Android Development--Common sensor summary

Source: Internet
Author: User

With the development of mobile phones, there are now more and more sensor types supported by mobile phones, in the development of the use of sensors to carry out some of the operation is a refreshing feeling, such as shaking in the wave, as well as the mobile phone music player in the shake-cut song. Here's a brief introduction to the use of sensors in Android and some commonly used sensors today.

First, the use of sensors

1. First we need to obtain the Sensor management object: Through Msensormanager = (Sensormanager) getsystemservice (Context.sensor_service);

2. To add a listener event for the Sensormanager object, the Sensormanage Registerlistener method can be used to add a listener event to our sensor, which has several overloaded forms Generally we have to provide a listener object and implement the Sensoreventlistener interface, then the type of the sensor (int type value). or a Sensor sensors object (you can get one by getdefaultsensor).

There are two important methods in the Sensoreventlistener interface,

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

2) public void onsensorchanged (Sensorevent event), the sensor information is changed when the method is executed, where the values member of the event object contains important information about the sensor we need, which is a float array value , generally depending on the sensor type, the value will be different, I will introduce some of the common sensor types of values worth meaning. In general, we handle our own things in this method based on the sensor information obtained.

3. When we do not need to monitor the sensor, we use a Unregisterlistener method to destroy the registration.

Ii. Introduction of common sensor types

In front of us we already know that the values in Sensorevent are an important variable to get the sensor information, and then introduce the values of some common sensors.

Along the phone screen is the x axis, right is positive, along the phone screen vertical to the y axis, upward is positive, vertical phone screen orientation is z axis, outward positive.

1.sensor.type_accelerometer: Accelerometer Sensor

Values[0]: The gravitational acceleration in the x-axis direction.

VALUES[1]: The gravitational acceleration in the y-axis direction.

VALUES[2]: The gravitational acceleration in the z-axis direction.

2.sensor.type_gyroscope: Gyro sensor

Values[0]: angular velocity rotated along the x-axis.

VALUES[1]: angular velocity rotated along the y axis.

VALUES[2]: angular velocity rotated along the z axis.

3.sensor.type_orientation: Direction sensor

The rotation vector represents the device's combined direction angle and the axis of the device is rotated by a corner θ around the axis < x, y, z >.

       values[0]: Mobile phone around z axis rotation angle. 0 North ( north ); 90 east 180 south 270 west ). If the phone is placed horizontally, it means that the angle between the front of the phone and the north direction is the value.

VALUES[1]: the angle at which the phone rotates around the x-axis. The range of values is between -180 and +180 degrees.

VALUES[2]: the angle at which the phone rotates around the y axis. The range of values is between -90 and +90 degrees.

4.sensor.type_light: Light sensor

Values[0]: Ambient light level lux unit value.

5.sensor.type_pressure: Pressure sensor

Values[0]: Atmospheric pressure value, unit kpa.

6.sensor.type_proximity: Proximity sensor

Values[0]: The distance value in centimeters.

7.sensor.type_relative_humidity: Humidity Sensor

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

8.sensor.type_temperature: temperature sensor

VALUES[0]: Celsius value of ambient temperature.

The Android API defines more than 10 types of sensors, each of which cannot support all sensor types, indicating that the phone does not support this type of sensor when the return value is null by the Getdefaultsensor method, when we register a listener event for an unsupported sensor, It does not throw an exception, but does not get any valid data.

Third, the practice of acceleration sensor to achieve shake-shake function

Next, we use the code to simulate a shake of the function, mainly through the accelerometer to achieve, when the event is triggered, we call the phone vibrator, and a toast display. You need to add permissions to our app when calling vibrate alerts.

<uses-permission android:name= "Android.permission.VIBRATE"/>
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.vibrator_service);//Get 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 a list Ener, the second parameter is the resulting sensor type, the third parameter value gets the frequency of the sensor information Msensormanager.registerlistener (Mysensorlistener, Msensormanager.getdefaultsensor (Sensor.type_accelerometer), sensormanager.sensor_delay_normal);// Sensormanager.sensor_delay_normal: The speed at which the sensor data is obtained by default. Sensormanager.sensor_delay_game: This value is recommended if you are developing a game using a sensor. SENSORMANAGER.SENSOR_DELAY_UI: This value is recommended if you are using 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//is called when the accuracy of the sensor changes, int is the new sensor precision} @Overridepublic void Onsens Orchanged (Sensorevent event) {//TODO auto-generated Method stub//The sensor information is changed when you execute it float[] values = event.values;//The variable has a maximum of 3 elements, and depending on the sensor, the elements in the values variable represent different meanings. float x = values[0]; Gravitational acceleration in x-axis float y = values[1]; The y-axis direction of the gravitational acceleration float z = values[2]; The gravitational acceleration of the z-axis//sets the gravitational acceleration value, which triggers the vibration when the shaking reaches the value, and corresponds to its own 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 PR Esent.getmenuinflater (). Inflate (R.menu.main, menu); return true;}}



Android Development--Common sensor summary

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.