Android uses sensors to get the data

Source: Internet
Author: User
Tags pow

Most Android phones have sensors, sensor types are direction, acceleration (gravity), light, magnetic field, distance (proximity), temperature, etc.

Direction sensor: Sensor.type_orientation

Acceleration (Gravity) Sensor: Sensor.type_accelerometer

Light sensor: Sensor.type_light

magnetic field Sensor: Sensor.type_magnetic_field

Distance (proximity) sensor: sensor.type_proximity

Temperature sensor: Sensor.type_temperature

Get some kind of sensor

Sensor sensor = Sensormanager.getdefaultsensor (Sensor.type_accelerometer);

Register for monitoring, get sensor change value

Sensormanager.registerlistener (listener, sensor, sensormanager.sensor_delay_game);

The third parameter above is the sample rate: fastest, game, normal, user interface. When an application requests a specific sample rate, it is only a recommendation to the sensor subsystem and does not guarantee that a particular sample rate is available.

Fastest: Sensormanager.sensor_delay_fastest

Minimum latency, generally not particularly sensitive handling is not recommended, this mode may cause a large consumption of mobile phone power, due to the transfer of raw data, the algorithm does not handle the game logic will affect the performance of the UI.

Game: Sensormanager.sensor_delay_game

Game latency, the majority of the most real-time high-level games are used in this category.

Normal: Sensormanager.sensor_delay_normal

Standard delay, for general puzzle or easy-level games can be used, but too low a sample rate may have some racing game with the jumping frame phenomenon.

User interface: SENSORMANAGER.SENSOR_DELAY_UI

Generally for the screen direction of automatic rotation use, relatively save energy and logic processing, general game development we do not use.

The difficulty of using the sensor is how to deal with the data and get the desired effect, which involves a lot of knowledge of mathematics and physics ...

Here's a code example that shows how to get the data for direction and field strength:

 Packagecom.magnetic;ImportAndroid.hardware.Sensor;Importandroid.hardware.SensorEvent;ImportAndroid.hardware.SensorEventListener;ImportAndroid.hardware.SensorManager;ImportAndroid.os.Bundle;Importandroid.app.Activity;ImportAndroid.view.Menu;ImportAndroid.widget.TextView; Public classMainactivityextendsActivity {PrivateTextView Magneticview; PrivateTextView Orientationview; PrivateTextView Totalmageticview; PrivateSensormanager Sensormanager; PrivateMysensoreventlistener Sensoreventlistener; @Overrideprotected voidonCreate (Bundle savedinstancestate) {Super. OnCreate (savedinstancestate);                Setcontentview (R.layout.activity_main); Sensoreventlistener=NewMysensoreventlistener (); Magneticview= (TextView) This. Findviewbyid (R.id.magneticview); Orientationview= (TextView) This. Findviewbyid (R.id.orientationview); Totalmageticview= (TextView) This. Findviewbyid (R.id.totalmageticview); //Get sensor ManagerSensormanager =(Sensormanager) Getsystemservice (Sensor_service); } @Overrideprotected voidOnresume () {//Get Direction sensor@SuppressWarnings ("Deprecation") Sensor Orientationsensor=sensormanager.getdefaultsensor (sensor.type_orientation);                        Sensormanager.registerlistener (Sensoreventlistener, Orientationsensor, sensormanager.sensor_delay_normal); //Get acceleration SensorSensor Accelerometersensor =sensormanager.getdefaultsensor (Sensor.type_magnetic_field);            Sensormanager.registerlistener (Sensoreventlistener, Accelerometersensor, sensormanager.sensor_delay_normal); Super. Onresume (); } @Override Public BooleanOncreateoptionsmenu (Menu menu) {//inflate the menu; This adds items to the action bar if it is present.getmenuinflater (). Inflate (R.menu.main, menu); return true; }        Private Final classMysensoreventlistenerImplementsSensoreventlistener {//can get the change value of the sensor in real-time measurement@Override Public voidonsensorchanged (Sensorevent event) {//get the value of the direction            if(Event.sensor.getType () = =sensor.type_orientation) {                    floatx =event.values[sensormanager.data_x]; floaty =event.values[sensormanager.data_y]; floatz =Event.values[sensormanager.data_z]; Orientationview.settext ("Direction:" + x + "," + y + "," +z); }                //get the value of acceleration            Else if(Event.sensor.getType () = =Sensor.type_magnetic_field) {                    floatx =event.values[sensormanager.data_x]; floaty =event.values[sensormanager.data_y]; floatz =Event.values[sensormanager.data_z]; Magneticview.settext ("Combined magnetic field strength X, Y, Z components:" + x + "," + Y + "," +z); //calculation and magnetic field strength                floatTotal= (float) (Math.sqrt (Math.pow (x,2) +math.pow (y,2) +math.pow (Z, 2))); Totalmageticview.settext ("Combined magnetic field strength:" +Total ); }          }            //Rewrite changes@Override Public voidOnaccuracychanged (sensor sensor,intaccuracy) {            }        }           //pausing the capture of a sensor@Overrideprotected voidOnPause () {Sensormanager.unregisterlistener (Sensoreventlistener); Super. OnPause (); }            }
<?xml version= "1.0" encoding= "Utf-8"?><tablelayout xmlns:android= "Http://schemas.android.com/apk/res/android"Android:layout_width= "Fill_parent"Android:layout_height= "Fill_parent" > <TableRow> <TextView Android:id= "@+id/orientationview"Android:text= "Direction sensor:"Android:layout_width= "Wrap_content"Android:layout_height= "Wrap_content"Android:layout_weight= "1"/> </TableRow> <TableRow> <TextView Android:id= "@+id/magneticview"Android:text= "Magnetic Sensor:"Android:layout_width= "Wrap_content"Android:layout_height= "Wrap_content"/> </TableRow> <TableRow> <TextView Android:id= "@+id/totalmageticview"Android:text= "Magnetic Strength:"Android:layout_width= "Wrap_content"Android:layout_height= "Wrap_content"/> </TableRow></TableLayout>

Android uses sensors to get the data

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.