Android sensor and android Sensor

Source: Internet
Author: User

Android sensor and android Sensor

1. Illumination Sensor

Activity_main.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:layout_width="match_parent"    android:layout_height="match_parent">    <TextView        android:id="@+id/light_level"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:layout_centerInParent="true"         /></RelativeLayout>

MainActivity. java

Package liqing. lightsensortest; 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. support. v7.app. appCompatActivity; import android. widget. textView; public class MainActivity extends AppCompatActivity {private SensorManager sensorMana Ger; private TextView lightLevel; @ Override protected void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); setContentView (R. layout. activity_main); lightLevel = (TextView) findViewById (R. id. light_level); // obtain the system sensor manager sensorManager = (SensorManager) getSystemService (Context. SENSOR_SERVICE); // obtain the illumination Sensor sensor Sensor = sensorManager. getdefasensensor (Sensor. TYPE_LIGHT); // illumination Sensor Registration listener. The parameter is SensorEventListener instance, sensor object, and output information rate. registerListener (listener, sensor, SensorManager. SENSOR_DELAY_NORMAL);} private SensorEventListener listener = new SensorEventListener () {@ Override public void onSensorChanged (SensorEvent event) {float value = event. values [0]; lightLevel. setText ("Current light level is" + value + "lx") ;}@ Override public void onAccuracy Changed (Sensor sensor, int accuracy) {};@ Override protected void onDestroy () {super. onDestroy (); if (sensorManager! = Null) {// deregister the listener sensorManager. unregisterListener (listener );}}}

2. Accelerator sensor (shake)

Activity_main.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:layout_width="match_parent"    android:layout_height="match_parent">    <TextView        android:id="@+id/light_level"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:layout_centerInParent="true"        /></RelativeLayout>

MainActivity. java

Public class MainActivity extends AppCompatActivity {private SensorManager sensorManager; private TextView lightLevel; @ Override protected void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); setContentView (R. layout. activity_main); lightLevel = (TextView) findViewById (R. id. light_level); sensorManager = (SensorManager) getSystemService (Context. SENSOR_SERVICE); // obtain the acceler Ometer (accelerator) Sensor sensor Sensor = sensorManager. getdefasensensor (Sensor. TYPE_ACCELEROMETER); sensorManager. registerListener (listener, sensor, SensorManager. SENSOR_DELAY_NORMAL);} private SensorEventListener listener = new SensorEventListener () {@ Override public void onSensorChanged (SensorEvent event) {// The value of the sensor SensorEvent in the direction is the X, Y, and Z axes, math. abs () float x = Math. abs (event. values [0]); float y = Math. Abs (event. values [1]); float z = Math. abs (event. values [2]); if (x> 15 | y> 15 | z> 15) {lightLevel. setText ("Shake") ;}}@ Override public void onAccuracyChanged (Sensor sensor, int accuracy) {}}; @ Override protected void onDestroy () {super. onDestroy (); if (sensorManager! = Null) {// deregister the listener sensorManager. unregisterListener (listener );}}}

3. The orientation sensor (car game control direction) uses the acceleration sensor and the Geomagnetic sensor to get the XYZ rotation value through getRotationMatrix () and getOrientation ()

Main_activity.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:layout_width="match_parent"    android:layout_height="match_parent">    <TextView        android:id="@+id/light_level"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:layout_centerInParent="true"         /></RelativeLayout>

MainActivity. java

Package liqing. lightsensortest; 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. support. v7.app. appCompatActivity; import android. widget. textView; public class MainActivity extends AppCompatActivity {private SensorManager sensorMana Ger; private TextView textView; @ Override protected void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); setContentView (R. layout. activity_main); textView = (TextView) findViewById (R. id. light_level); sensorManager = (SensorManager) getSystemService (Context. SENSOR_SERVICE); // obtain the accelerometer (accelerator) Sensor accelerometerSensor = sensorManager. getdefasensensor (Sensor. TYPE_AC CELEROMETER); // to magnetic_FIELD (geomagnetic _ field) Sensor magneticSensor = sensorManager. getdefasensensor (Sensor. TYPE_MAGNETIC_FIELD); // note that the speed of the SENSOR_DELAY_GAME sensor changes quite fast. registerListener (listener, accelerometerSensor, SensorManager. SENSOR_DELAY_GAME); sensorManager. registerListener (listener, magneticSensor, SensorManager. SENSOR_DELAY_GAME);} private SensorEventListener listener = new Sen SorEventListener () {float [] accelerometerValues = new float [3]; float [] magneticValues = new float [3]; @ Override public void onSensorChanged (SensorEvent event) {// note that SensorEvent is the sensor information (including the type and value) switch (event. sensor. getType () {case Sensor. TYPE_ACCELEROMETER: // clone () clone accelerometerValues = event. values. clone (); break; case Sensor. TYPE_MAGNETIC_FIELD: // clone () clone magneticValues = event. values. Clone (); break; default: break;} float [] R = new float [9]; // The Matrix value float [] values = new float [3]; // X, Y, and Z rotation values // getRotationMatrix (get rotation matrix) SensorManager. getRotationMatrix (R, null, accelerometerValues, magneticValues); // getOrientation (obtain the selected value) SensorManager. getOrientation (R, values); textView. setText ("X =" + values [0] + "\ n" + "Y =" + values [1] + "\ n" + "Z =" + values [2] + "\ n ");} @ Override public void OnAccuracyChanged (Sensor sensor, int accuracy) {}}; @ Override protected void onDestroy () {super. onDestroy (); if (sensorManager! = Null) {// deregister the listener sensorManager. unregisterListener (listener );}}}

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.