Android practice simple tutorial-62nd guns (electronic Sensor exact Compass)
Here we use the sensor provided by the mobile phone to implement a simple small example of the electronic compass. You can learn how to use the SensorManager class, SensorEventListener and its overwriting method.
First, create a layout file:
Then the Activity file:
Package com. yayun. activity; import android. app. activity; import android. hardware. sensor; import android. hardware. sensorEvent; import android. hardware. sensorEventListener; import android. hardware. sensorManager; import android. OS. bundle; import android. widget. textView; public class SensorDemo extends Activity {private TextView mShowTextView; private SensorManager mSensorManager;/** Called when the activity is first created. * // @ Override public void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); setContentView (R. layout. main); mShowTextView = (TextView) findViewById (R. id. TV _show);/* Get SensorManager */mSensorManager = (SensorManager) getSystemService (SENSOR_SERVICE) ;}@ Override protected void onResume () {super. onResume ();/* obtain the sensitive Sensor and register the SensorEventListener */mSensorManager. registerListener (mSensorEventListener, mSensorManager. getdefasensensor (Sensor. TYPE_ORIENTATION), SensorManager. SENSOR_DELAY_NORMAL) ;}@ Override protected void onPause () {/* cancel registering SensorEventListener */mSensorManager. unregisterListener (mSensorEventListener); super. onPause ();} private final SensorEventListener mSensorEventListener = new SensorEventListener () {@ Override public void listener (Sensor sensor, int accuracy) {}@ Override public void onSensorChanged (SensorEvent event) // transform {/* determine the Sensor type */if (event. sensor. getType () = Sensor. TYPE_ORIENTATION) {/* obtain X-value data */float x_data = event. values [SensorManager. DATA_X]; if (x_data> 0 & x_data <= 22.5) | x_data> 337.5) {mShowTextView. setText (Northern + String. valueOf (x_data);} else if (x_data> 22.5 & x_data <= 67.5) {mShowTextView. setText (North East + String. valueOf (x_data);} else if (x_data> 67.5 & x_data <= 112.5) {mShowTextView. setText (Oriental + String. valueOf (x_data);} else if (x_data> 112.5 & x_data <= 157.5) {mShowTextView. setText (East-South + String. valueOf (x_data);} else if (x_data> 157.5 & x_data <= 202.5) {mShowTextView. setText (Southern + String. valueOf (x_data);} else if (x_data> 202.5 & x_data <= 247.5) {mShowTextView. setText (southern West + String. valueOf (x_data);} else if (x_data> 247.5 & x_data <= 292.5) {mShowTextView. setText (western + String. valueOf (x_data);} else if (x_data> 292.5 & x_data <= 337.5) {mShowTextView. setText (North West + String. valueOf (x_data ));}}}};}
Here we mainly use the following methods:
1. public boolean registerListener (SensorEventListener listener, Sensor sensor, int samplingPeriodUs)
Parameters
Listener |
ASensorEventListener Object. // SensorEventListener object |
Sensor |
TheSensor To register. |
SamplingPeriodUs |
The ratesensor events Are delivered at. This is only a hint to the system. Events may be stored ed faster or slower than the specified rate. Usually events are stored ed faster. The value must be oneSENSOR_DELAY_NORMAL ,SENSOR_DELAY_UI ,SENSOR_DELAY_GAME , OrSENSOR_DELAY_FASTEST Or, the desired delay between events in microseconds. Specifying the delay in microseconds only works from Android 2.3 (API level 9) onwards. For earlier releases, you must use one ofSENSOR_DELAY_* Constants. |
Returns
true
If the sensor is supported and successfully enabled. the preceding samplingPeriodUs has three values:
SENSOR_DELAY_GAME this value is recommended if a game is developed using sensors. This level is generally used by games with high real-time performance.
SENSOR_DELAY_NORMAL: the default speed for obtaining sensor data. Standard latency can be used for general games of the benefit and intelligence type or EASY sector, but a low sampling rate may cause frame skipping for some racing games.
SENSOR_DELAY_UI this value is recommended if the sensor is used to update the UI.
SENSOR_DELAY_FASTEST: the lowest latency. This mode is generally not recommended for sensitive processing. This mode may cause a large amount of power consumption on mobile phones, and because it transmits a large amount of raw data, poor algorithm processing will affect the game logic and UI performance.
2. public void unregisterListener (SensorEventListener listener) // cancel registration
Always make sure to disable sensors you don't need, especially when your activity is paused. failing to do so can drain the battery in just a few hours. note that the system will not disable sensors automatically when the screen turns off.
As you can see, this document requires that we try to unregister unnecessary sensors, especially when our activity is in a state of losing focus. If we do not follow the above steps, the cell phone battery will soon be used up.
Note that the sensor will not automatically unregister when the screen is off.
Therefore, we can use the onPause () method and onresume () method in the activity. Register the listener for the sensor in onresume method I, in onPause ()
Method.
3. onSensorChanged (SensorEvent event) method of SensorEventListener
First, determine the types of sensors, including:
Int |
TYPE_ACCELEROMETER |
A constant describing an accelerometer sensor type. Acceleration sensor |
Int |
TYPE_ALL |
A constant describing all sensor types. all types A constant describing all sensor types. |
Int |
TYPE_GRAVITY |
A constant describing a gravity sensor type. |
Int |
TYPE_GYROSCOPE |
A constant describing a gyroscope sensor type rotator sensor |
Int |
TYPE_LIGHT |
A constant describing an light sensor type. light sensor |
Int |
TYPE_LINEAR_ACCELERATION |
A constant describing a linear acceleration sensor type. |
Int |
TYPE_MAGNETIC_FIELD |
A constant describing a magnetic field sensor type. magnetic field sensor |
Int |
TYPE_ORIENTATION |
This constant is deprecated. useSensorManager.getOrientation() Instead.Magnetic Field Sensor |
Int |
TYPE_PRESSURE |
A constant describing a pressure sensor type pressure gauge sensor |
Int |
TYPE_PROXIMITY |
A constant describing an proximity sensor type. distance sensor |
Int |
TYPE_ROTATION_VECTOR |
A constant describing a rotation vector sensor type. |
Int |
TYPE_TEMPERATURE |
A constant describing a temperature sensor type temperature sensor |
Then, obtain the angle value based on float x_data = event. values [SensorManager. DATA_X]; and determine the angle value. There are several constants that you can call as needed.