Android learning teaches you how to deal with sensor in five steps to make the program Cooler

Source: Internet
Author: User

First, we will introduce what a sensor is.

Sensor (sensor ):
Wikipedia tells me:SensorIt is a device that receives signals or stimulates and reacts. It can convert the physical quantity or chemical quantity to another suitable output.

For example, gravity sensor and direction Sensor

Android abstracts the sensors of each device. The sensormanger class is used to control the sensor, the sensor class is used to describe the specific sensor, and the sensoreventlistener class is used to monitor the changes in the sensor value.

Enter the topic:

 

You only need five steps to get the sensor. Make your program cooler.

 

// Step 1: Obtain the sensormanager object and return the controller of a hardware device <br/> sensormanager = (sensormanager) getsystemservice (SERVICE_NAME); <br/> // Step 2: obtain a specific sensor <br/> Sensor accelersensor = sensormanager. getdefasensensor (sensor. type_accelerometer); <br/> // Step 3: Create a sensoreventlistener to listen for changes in the sensor value and perform the corresponding action <br/> sensoreventlistener mysensorlistener = new sensoreventlistener () {<br/> // call this method to change the sensor value <br/> @ override <br/> publi C void onsensorchanged (sensorevent event) {<br/> float x = event. values [0]; <br/> float y = event. values [1]; <br/> float z = event. values [2]; <br/> // after obtaining the value, you can perform corresponding processing. <br/> system. out. println ("~~~ X is: "+ x); <br/> system. out. println ("--- y is:" + Y); <br/> system. out. println ("++ Z is:" + Z ); <br/>}< br/> // call this method to change the sensor's precision <br/> @ override <br/> Public void onaccuracychanged (sensor, int accuracy) {<br/>}< br/> // Step 4: register the sensor event monitoring event <br/> sensormanager. registerlistener (mysensorlistener, accelersensor, sensormanager. sensor_delay_normal); <br/> // Step 5: cancel the sensor event listening <br/> sensormanager. unregisterlistener (mysensorlistener ); 

Supplement:

 

The following sensors can be obtained in step 2,

 

Sensor Type list:

Sensor. type_accelerometer:

Accelerometer Sensor

Sensor. type_gyroscope:

Rotator Sensor

Sensor. type_light:

Light sensors dynamically control screen brightness

Sensor. type_magnetic_field:

Magnetic Field Sensor

Sensor. type_orientation:

Direction Sensor

Sensor. type_pressure:

Pressure Sensor

Sensor. type_proximiy:

Proximity sensor

Sensor. type_temperature:

Temperature Sensor

 

Some mobile phones do not support some sensor devices. You can determine whether the sensor is null. If it is null, You can prompt the user.


You can also use the following statement to obtain a list of available sensors for the device:

List <Sensor> allsensors = sensormanger. getsensorlist (sensor. type_all );

 

In step 3,

The three parameters are sensoreventlistener, sensor, and sensor update rate.

 

The first two parameters are previously declared.

 

The third parameter has four options.

Sensor update rate:

Sensormanager. sensor_delay_fastest:

Specify the fastest possible sensor update rate

 

Sensormanager. sensor_delay_game:

Specify the update rate suitable for use in the game

 

Sensormanager. sensor_delay_normal:

Specify the default update rate

 

Sensormanager. sensor_delay_ui:

Specifies the update rate suitable for UI updates.

 

 

The fourth step is generally in onresume.

 

Step 5 is generally performed in onpause.

In this way, activities use them only when they interact with users.

 

I wrote a simple example of a direction sensor. If you are interested, download:

Http://download.csdn.net/source/3326822

 

In this way, you can enter the android sensor world. In the following article, I will introduce some sensors in detail based on games and instances.

It's not too early. We're here today. Thank you.

 

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.