Android SensorManager and sensormanager
Most Android devices have built-in sensors, some of which are hardware-based and some are software-based. Sensors can be used to monitor the 3D Motion and position of a device, and to infer user actions, such as skew, vibration, and rotation, and to monitor changes in the environment around the device.
On the Android platform, we can access and listen to sensors through SensorManager, and obtain relevant information. At the same time, we provide some sensor constants for sensor accuracy and acquisition rate. Common SensorManager methods include:
1. Sensor getdefasensensor (int type): gets the Default Sensor of the specified type.
2. List <Sensor> getSensorList (int type): obtains all the sensors of the specified type. If you want to obtain all the sensors, You can input Sensor. TYPE_ALL.
3. RegisterListener (SensorEventListener) listensor, Sensor sensor, int rate): registers a listener for a Sensor.
4. UnregisterListener (SensorEventListener listener, Sensor sensor): cancels the Sensor listener.
The Sensor class represents a Sensor, which contains multiple constants. You can specify the Sensor type and provide corresponding methods to obtain various attribute values of the Sensor.
Methods:
1. getMaximumRange (): maximum value range.
2. getName (): Device Name
3. getPower (): Power
4. getResolution (): Precision
5. getType (): Sensor Type
6. getVendor (): Device supplier
7. getVersion (): device version
The SensorEvent class is used to describe sensor events and encapsulate information about sensor events. The information includes sensor type, time, accuracy, and sensor raw data.
SensorEventListener: used to listen to a sensor. When the sensor's precision or data changes, the corresponding method is called back.
The following example shows how to use the device. First, obtain all the sensor information of the device. The key code is as follows:
Public void getSensors (View view) {// obtain the List of all sensors in the mobile phone <Sensor> sensors = mSensorManager. getSensorList (Sensor. TYPE_ALL); StringBuilder sb = new StringBuilder (); for (int I = 0; I <sensors. size (); I ++) {Sensor s = sensors. get (I); sb. append ("name :"). append (s. getName ()). append ("\ n"); sb. append ("type :"). append (s. getType ()). append (""); String type = "unknown sensor"; switch (s. getType () {case Sensor. TYPE_ACCELEROMETER: type = "accelerometer"; type = "TYPE_ACCELEROMETER"; break; case Sensor. TYPE_AMBIENT_TEMPERATURE: type = "TYPE_AMBIENT_TEMPERATURE"; break; case Sensor. TYPE_GAME_ROTATION_VECTOR: type = "TYPE_GAME_ROTATION_VECTOR"; break; case Sensor. TYPE_GRAVITY: type = "TYPE_GRAVITY"; break; case Sensor. TYPE_GEOMAGNETIC_ROTATION_VECTOR: type = "TYPE_GEOMAGNETIC_ROTATION_VECTOR"; break; case Sensor. TYPE_GYROSCOPE: type = "gyroscope Sensor"; type = "TYPE_GYROSCOPE"; break; case Sensor. TYPE_GYROSCOPE_UNCALIBRATED: type = "TYPE_GYROSCOPE_UNCALIBRATED"; break; case Sensor. TYPE_HEART_RATE: type = "TYPE_HEART_RATE"; break; case Sensor. TYPE_LIGHT: type = "ambient light Sensor"; type = "TYPE_LIGHT"; break; case Sensor. TYPE_MAGNETIC_FIELD: type = "Electromagnetic Field Sensor"; type = "TYPE_MAGNETIC_FIELD"; break; case Sensor. TYPE_LINEAR_ACCELERATION: type = "TYPE_LINEAR_ACCELERATION"; break; case Sensor. TYPE_PRESSURE: type = "Pressure Sensor"; type = "TYPE_PRESSURE"; break; case Sensor. TYPE_PROXIMITY: type = "distance Sensor"; type = "TYPE_PROXIMITY"; break; case Sensor. TYPE_ORIENTATION: type = "direction Sensor"; type = "TYPE_ORIENTATION"; break; case Sensor. TYPE_TEMPERATURE: type = "Temperature Sensor"; type = "TYPE_TEMPERATURE"; break; case Sensor. TYPE_STEP_COUNTER: type = "TYPE_STEP_COUNTER"; break; case Sensor. TYPE_ROTATION_VECTOR: type = "TYPE_ROTATION_VECTOR"; break;} sb. append (type ). append ("\ n"); sb. append ("version :"). append (s. getVersion ()). append ("\ n"); sb. append ("supplier :"). append (s. getVendor ()). append ("\ n"); sb. append ("\ n");} show. setText (sb. toString ());}
As follows:
The following code uses the monitoring acceleration sensor to perform the shake operation:
Public class sens?ti=extends ActionBarActivity {private SensorManager sensorManager; private Vibrator vibrator; @ Override protected void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); setContentView (R. layout. activity_sensor); sensorManager = (SensorManager) getSystemService (Context. SENSOR_SERVICE); vibrator = (Vibrator) getSystemService (Context. VIBRATOR_SERVICE) ;}@ Override protected void onResume () {super. onResume (); Sensor sensor = sensorManager. getdefasensensor (Sensor. TYPE_ACCELEROMETER); sensorManager. registerListener (listener, sensor, SensorManager. SENSOR_DELAY_NORMAL) ;}@ Override protected void onPause () {super. onPause (); sensorManager. unregisterListener (listener);} private SensorEventListener listener = new SensorEventListener () {@ Override public void onSensorChanged (SensorEvent sensorEvent) {// float [] v = sensorEvent. values; int sValue = 20; float x = v [0]; // acceleration float y = v [1] In the x direction; // float z = v [2] In y direction; // if (Math. abs (x)> sValue) {vibrator. vibrate (300);} if (Math. abs (y)> sValue) {vibrator. vibrate (300);} if (Math. abs (z)> sValue) {vibrator. vibrate (300); }}@ Override public void onAccuracyChanged (Sensor sensor, int I) {// Method for changing the precision }};}
Author: Jerry Education
Source: http://www.cnblogs.com/jerehedu/
Copyright Disclaimer: The copyright of this article is shared by Yantai jereh Education Technology Co., Ltd. and the blog Park. You are welcome to repost it. However, you must keep this statement without the consent of the author and provide the original article connection clearly on the article page, otherwise, you are entitled to pursue legal liability.
Technical Consultation: