The sensor is a detection device that can feel the information being measured, and can transform the detected and sensed information into an electrical signal or other required form of information output according to a certain law.
There are many sensors (physical devices) built into the Android operating system that can detect, perceive, and communicate the signals and physical conditions of the outside world to other devices.
For example, some games or software can automatically identify the screen to change the layout of screen display
Here are some of the sensors supported by Android:
Acceleration Sensor Sensor.type_accelerometerGyro sensor Sensor.type_gyroscopeAmbient light sensor sensor.type_lightelectromagnetic field sensor Sensor.type_magnetic_fieldDirection sensor sensor.type_orientation:pressure sensor sensor.type_pressure:distance sensor sensor.type_proximity:temperature sensor sensor.type_temperature:
Run:
Program Structure:
<?xml version= "1.0" encoding= "Utf-8"? ><linearlayout xmlns:android= "Http://schemas.android.com/apk/res/android"Xmlns:app= "Http://schemas.android.com/apk/res-auto"Xmlns:tools= "Http://schemas.android.com/tools"android:orientation= "Vertical"Android:layout_width= "Fill_parent"Android:layout_height= "Fill_parent"Tools:context= "Com.example.asus.gary_01.MainActivity" > <TextView android:layout_width= "Fill_parent"Android:layout_height= "Wrap_content"Android:text= "Sensor operation!" "android:textsize= "10pt"/> <Button Android:id= "@+id/button"Android:layout_width= "Match_parent"Android:layout_height= "Wrap_content"Android:text= "Get phone sensor information"/> <TextView Android:id= "@+id/textview"Android:scrollbars= "Vertical"Android:layout_width= "Fill_parent"Android:layout_height= "Fill_parent"android:textsize= "8pt"/></linearlayout>
Activity_main
Packagecom.example.asus.gary_01;ImportAndroid.content.Context;ImportAndroid.hardware.Sensor;ImportAndroid.hardware.SensorManager;Importandroid.support.v7.app.AppCompatActivity;ImportAndroid.os.Bundle;ImportAndroid.text.method.ScrollingMovementMethod;ImportAndroid.view.View;ImportAndroid.widget.Button;ImportAndroid.widget.TextView;Importjava.util.List; Public classMainactivityextendsappcompatactivity {PrivateTextView tx1; @Overrideprotected voidonCreate (Bundle savedinstancestate) {Super. OnCreate (savedinstancestate); Setcontentview (R.layout.activity_main); Button BT1=(Button) Findviewbyid (R.id.button); TX1=(TextView) Findviewbyid (R.id.textview); //get the Sensor manager from the system FinalSensormanager SM =(Sensormanager) Getsystemservice (Context.sensor_service); Bt1.setonclicklistener (NewView.onclicklistener () {@Override Public voidOnClick (View v) {String str; //get the full list of sensors from the sensor managerList<sensor> allsensors =sm.getsensorlist (Sensor.type_all); inti; //Add scroll bars to ViewtextTx1.setmovementmethod (Scrollingmovementmethod.getinstance ()); //shows how many sensors there areTx1.settext ("The mobile phone has a" +allsensors.size () + "sensor, respectively, they are:"); Sensor s; //display specific information for each sensor for(I=0;i<allsensors.size (); i++) {s=Allsensors.get (i); STR= "Device Name:" +S.getname (); Switch(S.gettype ()) {//Acceleration Sensor Sensor.type_accelerometer CaseSensor.TYPE_ACCELEROMETER:tx1.setText (Tx1.gettext ()+ "\ n" +i+ "acceleration sensor accelerometer:\n" +str); Break; //Gyro sensor Sensor.type_gyroscope CaseSensor.TYPE_GYROSCOPE:tx1.setText (Tx1.gettext ()+ "\ n" +i+ "Gyro sensor gyroscope:\n" +str); Break; //Ambient light sensor sensor.type_light CaseSensor.TYPE_LIGHT:tx1.setText (Tx1.gettext ()+ "\ n" +i+ "Ambient light sensor light:\n" +str); Break; //electromagnetic field sensor Sensor.type_magnetic_field CaseSensor.TYPE_MAGNETIC_FIELD:tx1.setText (Tx1.gettext ()+ "\ n" +i+ "electromagnetic field sensor magnetic:\n" +str); Break; //Direction sensor sensor.type_orientation: CaseSensor.TYPE_ORIENTATION:tx1.setText (Tx1.gettext ()+ "\ n" +i+ "Direction sensor orientation:\n" +str); Break; //pressure sensor sensor.type_pressure: CaseSensor.TYPE_PRESSURE:tx1.setText (Tx1.gettext ()+ "\ n" +i+ "Pressure sensor pressure:\n" +str); Break; //distance sensor sensor.type_proximity: CaseSensor.TYPE_PROXIMITY:tx1.setText (Tx1.gettext ()+ "\ n" +i+ "Distance sensor proximity:\n" +str); Break; //temperature sensor sensor.type_temperature: CaseSensor.TYPE_TEMPERATURE:tx1.setText (Tx1.gettext ()+ "\ n" +i+ "Temperature sensor temperature:\n" +str); Break; default: Tx1.settext (Tx1.gettext ()+ "\ n" +i+ "Unknown sensor: \ n" +str); Break; } } } }); }}
mainactivity
First, the interface layout
Two TextView, one button, below the TextView support scrolling operation
Click button to get the phone sensor and display it on the textview below
Add scroll bar to TextView: Portal
XML code:
Set the orientation of the scroll bar
android:scrollbars= "Vertical"
Settings in Java
tx1= (TextView) Findviewbyid (R.ID.TV1);
Set scrolling mode
Tx1.setmovementmethod (Scrollingmovementmethod.getinstance ());
II. Realization of the procedure
Get the sensor manager from the system
Final Sensormanager sm = (Sensormanager) getsystemservice (Context.sensor_service);
Get the full list of sensors from the sensor manager
list<sensor> allsensors = sm.getsensorlist (Sensor.type_all);
Bt1.setonclicklistener (NewView.onclicklistener () {@Override Public voidOnClick (View v) {String str; //get the full list of sensors from the sensor managerList<sensor> allsensors =sm.getsensorlist (Sensor.type_all); inti; //Add scroll bars to ViewtextTx1.setmovementmethod (Scrollingmovementmethod.getinstance ()); //shows how many sensors there areTx1.settext ("The mobile phone has a" +allsensors.size () + "sensor, respectively, they are:"); Sensor s; //display specific information for each sensor for(I=0;i<allsensors.size (); i++) {s=Allsensors.get (i); STR= "Device Name:" +S.getname (); Switch(S.gettype ()) {//Acceleration Sensor Sensor.type_accelerometer CaseSensor.TYPE_ACCELEROMETER:tx1.setText (Tx1.gettext ()+ "\ n" +i+ "acceleration sensor accelerometer:\n" +str); Break; //Gyro sensor Sensor.type_gyroscope CaseSensor.TYPE_GYROSCOPE:tx1.setText (Tx1.gettext ()+ "\ n" +i+ "Gyro sensor gyroscope:\n" +str); Break; //Ambient light sensor sensor.type_light CaseSensor.TYPE_LIGHT:tx1.setText (Tx1.gettext ()+ "\ n" +i+ "Ambient light sensor light:\n" +str); Break; //electromagnetic field sensor Sensor.type_magnetic_field CaseSensor.TYPE_MAGNETIC_FIELD:tx1.setText (Tx1.gettext ()+ "\ n" +i+ "electromagnetic field sensor magnetic:\n" +str); Break; //Direction sensor sensor.type_orientation: CaseSensor.TYPE_ORIENTATION:tx1.setText (Tx1.gettext ()+ "\ n" +i+ "Direction sensor orientation:\n" +str); Break; //pressure sensor sensor.type_pressure: CaseSensor.TYPE_PRESSURE:tx1.setText (Tx1.gettext ()+ "\ n" +i+ "Pressure sensor pressure:\n" +str); Break; //distance sensor sensor.type_proximity: CaseSensor.TYPE_PROXIMITY:tx1.setText (Tx1.gettext ()+ "\ n" +i+ "Distance sensor proximity:\n" +str); Break; //temperature sensor sensor.type_temperature: CaseSensor.TYPE_TEMPERATURE:tx1.setText (Tx1.gettext ()+ "\ n" +i+ "Temperature sensor temperature:\n" +str); Break; default: Tx1.settext (Tx1.gettext ()+ "\ n" +i+ "Unknown sensor: \ n" +str); Break; } } } });
ANDROID_ (sensor) Gets the sensor in the phone