Android action sensor and android Sensor

Source: Internet
Author: User

Android action sensor and android Sensor

Android motion sensor

The Android platform supports some sensors used to monitor device movements (five such sensors ). Two of them are pure hardware sensors. The other three (Gravity Sensors, linear acceleration sensors, and Rotation Vector Sensors) may be hardware sensors or software sensors. For example, in some Android devices, software-based sensors obtain data from acceleration and magnetic sensors, but in other Android devices, they may also obtain data from gyroscope sensors. That is to say, the data returned by the same software-based sensor on different Android devices may come from different hardware sensors. Therefore, software-based sensors may vary in precision and scope of use in different devices. Most high-end Android devices have acceleration sensors and some have gyroscope sensors.

Motion sensors are very useful for the movement of monitoring devices. For example, tilt, vibration, rotation, and swing are all within the monitoring range of motion sensors. Device movement is usually a direct response to user input. For example, a user is racing in a game or controlling a ball in the game ). In addition, the physical environment of the device is also reflected in the action of the device. For example, the user is driving normally, and the Android device is quietly lying in the seat next to it, although the device does not move, it will continue to vibrate as the car moves, and the device will also move as the car moves.
In the first case, You can monitor the relative position of the device. In the second case, a reference system other than the device must be taken into account. The motion sensor itself is generally not used to monitor the location of the device. Other types of sensors are required to monitor the location of the device, such as magnetic field sensors.

All motion sensors return three floating-point values (returned through an array of 3), but for different sensors, these three values have different meanings. For example, the acceleration sensor returns data from three coordinate axes. For a gyroscope sensor, the rotation rate of the three axes is returned.

The device is pushed from left to right, and the X axis acceleration is positive.


The device is pushing towards itself, and the Y axis acceleration is positive.


If A m/s ^ 2 acceleration is pushed toward the sky, the acceleration of the Z axis is A + 9.81. Therefore, if the actual acceleration is calculated (to offset the acceleration of gravity), it needs to be reduced by 9.81.


View:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"    android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin"    android:paddingRight="@dimen/activity_horizontal_margin"    android:paddingTop="@dimen/activity_vertical_margin"    android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".MainActivity"    android:orientation="vertical">    <TextView android:id="@+id/sensor" android:layout_width="wrap_content"        android:layout_height="wrap_content" />    <TextView android:id="@+id/gravity" android:layout_width="wrap_content"        android:layout_height="wrap_content" />    <TextView android:id="@+id/PROXIMITY1" android:layout_width="wrap_content"        android:layout_height="wrap_content" /></LinearLayout>

Java:

Pupublic class MainActivity extends ActionBarActivity implements SensorEventListener {private TextView sensor = null; private SensorManager mSensorManager; private TextView gravityTV = null; private float [] gravity = new float [3]; @ Override protected void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); setContentView (R. layout. activity_main); sensor = (TextView) findViewById (R. id. sensor); mSensorManager = (SensorManager) getSystemService (SENSOR_SERVICE);} @ Override protected void onResume () {super. onResume (); mSensorManager. registerListener (this, mSensorManager. getdefasensensor (Sensor. TYPE_ACCELEROMETER), // acceleration sensor, SensorManager. SENSOR_DELAY_UI); // SensorManager. SENSOR_DELAY_UI sampling frequency mSensorManager. registerListener (this, mSensorManager. getdefasensensor (Sensor. TYPE_GRAVITY), // gravity sensor, SensorManager. SENSOR_DELAY_FASTEST); // SensorManager. SENSOR_DELAY_UI sampling frequency mSensorManager. registerListener (this, mSensorManager. getdefasensensor (Sensor. TYPE_PROXIMITY), // adjacent sensor SensorManager. SENSOR_DELAY_UI) ;}@ Override protected void onPause () {super. onPause (); mSensorManager. unregisterListener (this); // unmount all sensors} // sensor degree status change @ Override public void onSensorChanged (SensorEvent event) {switch (event. sensor. getType () {case Sensor. TYPE_ACCELEROMETER: // acceleration sensor // noise reduction (interference) final float alpha = (float) 0.8; gravity [0] = alpha * gravity [0] + (1-alpha) * event. values [0]; gravity [1] = alpha * gravity [1] + (1-alpha) * event. values [1]; gravity [2] = alpha * gravity [2] + (1-alpha) * event. values [2]; String accelerometer = String. valueOf ("X:" + (event. values [0]-gravity [0]) + "\ nY:" + (event. values [1]-gravity [1]) + "\ nZ:" + (event. values [2]-gravity [2]); sensor = (TextView) findViewById (R. id. sensor); sensor. setText (accelerometer); Log. I ("X:", String. valueOf (event. values [0]-gravity [0]); Log. I ("Y:", String. valueOf (event. values [1]-gravity [1]); Log. I ("Z:", String. valueOf (event. values [2]-gravity [2]); break; case Sensor. TYPE_GRAVITY: gravity [0] = event. values [0]; gravity [1] = event. values [1]; gravity [2] = event. values [2]; gravityTV = (TextView) findViewById (R. id. gravity); gravityTV. setText (String. valueOf ("gravity [0] =" + event. values [0] + "\ ngravity [1] =" + event. values [1] + "\ ngravity [2] =" + event. values [2]); break; case Sensor. TYPE_PROXIMITY: TextView Proximity = (TextView) findViewById (R. id. PROXIMITY1); Proximity. setText (String. valueOf (event. values [0]); break ;}// accuracy changes @ Override public void onAccuracyChanged (Sensor sensor, int accuracy ){}}


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.