• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1page.title=Sensors Overview
2parent.title=Sensors
3parent.link=index.html
4@jd:body
5
6<div id="qv-wrapper">
7  <div id="qv">
8    <h2>Quickview</h2>
9    <ul>
10      <li>Learn about the sensors that Android supports and the Android sensor framework.</li>
11      <li>Find out how to list sensors, determine sensor capabilities, and monitor sensor data.</li>
12      <li>Learn about best practices for accessing and using sensors.</li>
13    </ul>
14    <h2>In this document</h2>
15    <ol>
16      <li><a href="#sensors-intro">Introduction to Sensors</a></li>
17      <li><a href="#sensors-identify">Identifying Sensors and Sensor Capabilities</a></li>
18      <li><a href="#sensors-monitor">Monitoring Sensor Events</a></li>
19      <li><a href="#sensors-configs">Handling Different Sensor Configurations</a></li>
20      <li><a href="#sensors-coords">Sensor Coordinate System</a></li>
21      <li><a href="#sensors-practices">Best Practices for Accessing and Using Sensors</a></li>
22    </ol>
23    <h2>Key classes and interfaces</h2>
24    <ol>
25      <li>{@link android.hardware.Sensor}</li>
26      <li>{@link android.hardware.SensorEvent}</li>
27      <li>{@link android.hardware.SensorManager}</li>
28      <li>{@link android.hardware.SensorEventListener}</li>
29    </ol>
30    <h2>Related samples</h2>
31    <ol>
32      <li><a href="{@docRoot}resources/samples/AccelerometerPlay/index.html">Accelerometer
33      Play</a></li>
34      <li><a
35href="{@docRoot}resources/samples/ApiDemos/src/com/example/android/apis/os/RotationVectorDemo.html">
36API Demos (OS - RotationVectorDemo)</a></li>
37      <li><a
38href="{@docRoot}resources/samples/ApiDemos/src/com/example/android/apis/os/Sensors.html">API Demos
39(OS - Sensors)</a></li>
40    </ol>
41    <h2>See also</h2>
42    <ol>
43      <li><a href="{@docRoot}guide/topics/sensors/index.html">Sensors</a></li>
44      <li><a href="{@docRoot}guide/topics/sensors/sensors_motion.html">Motion Sensors</a></li>
45      <li><a href="{@docRoot}guide/topics/sensors/sensors_position.html">Position
46          Sensors</a></li>
47      <li><a href="{@docRoot}guide/topics/sensors/sensors_environment.html">Environment
48          Sensors</a></li>
49    </ol>
50  </div>
51</div>
52
53<p>Most Android-powered devices have built-in sensors that measure motion, orientation,
54and various environmental conditions. These sensors are capable of providing raw data with high
55precision and accuracy, and are useful if you want to monitor three-dimensional device movement or
56positioning, or you want to monitor changes in the ambient environment near a device. For example, a
57game might track readings from a device's gravity sensor to infer complex user gestures
58and motions, such as tilt, shake, rotation, or swing. Likewise, a weather application might use a
59device's temperature sensor and humidity sensor to calculate and report the dewpoint, or a travel
60application might use the geomagnetic field sensor and accelerometer to report a compass
61bearing.</p>
62
63<p>The Android platform supports three broad categories of sensors:</p>
64
65<ul>
66  <li>Motion sensors
67    <p>These sensors measure acceleration forces and rotational forces along three axes. This
68      category includes accelerometers, gravity sensors, gyroscopes, and rotational vector
69      sensors.</p>
70  </li>
71  <li>Environmental sensors
72    <p>These sensors measure various environmental parameters, such as ambient air temperature
73      and pressure, illumination, and humidity. This category includes barometers, photometers, and
74      thermometers.</p>
75  </li>
76  <li>Position sensors
77    <p>These sensors measure the physical position of a device. This category includes
78      orientation sensors and magnetometers.</p>
79  </li>
80</ul>
81
82
83<p>You can access sensors available on the device and acquire raw sensor data by using the Android
84sensor framework. The sensor framework provides several classes and interfaces that help you perform a wide
85variety of sensor-related tasks. For example, you can use the sensor framework to do the following:</p>
86
87<ul>
88   <li>Determine which sensors are available on a device.</li>
89   <li>Determine an individual sensor's capabilities, such as its maximum range, manufacturer, power
90     requirements, and resolution.</li>
91   <li>Acquire raw sensor data and define the minimum rate at which you acquire sensor data.</li>
92   <li>Register and unregister sensor event listeners that monitor sensor changes.</li>
93 </ul>
94
95<p>This topic provides an overview of the sensors that are available on the Android platform.
96It also provides an introduction to the sensor framework.</p>
97
98<h2 id="sensors-intro">Introduction to Sensors</h2>
99
100<p>The Android sensor framework lets you access many types of sensors. Some of these sensors are
101hardware-based and some are software-based. Hardware-based sensors are physical components built
102into a handset or tablet device. They derive their data by directly measuring specific environmental
103properties, such as acceleration, geomagnetic field strength, or angular change. Software-based
104sensors are not physical devices, although they mimic hardware-based sensors. Software-based sensors
105derive their data from one or more of the hardware-based sensors and are sometimes called virtual
106sensors or synthetic sensors. The linear acceleration sensor and the gravity sensor are examples of
107software-based sensors. Table 1 summarizes the sensors that are supported by the Android
108platform.</p>
109
110<p>Few Android-powered devices have every type of sensor. For example, most handset devices and
111tablets have an accelerometer and a magnetometer, but fewer devices have
112barometers or thermometers. Also, a device can have more than one sensor of a given type. For
113example, a device can have two gravity sensors, each one having a different range.</p>
114
115<p class="table-caption" id="table1">
116  <strong>Table 1.</strong> Sensor types supported by the Android platform.</p>
117<table>
118  <tr>
119    <th scope="col" style="white-space:nowrap">Sensor</th>
120    <th scope="col" style="white-space:nowrap">Type</th>
121    <th scope="col" style="white-space:nowrap">Description</th>
122    <th scope="col" style="white-space:nowrap">Common Uses</th>
123  </tr>
124  <tr>
125    <td>{@link android.hardware.Sensor#TYPE_ACCELEROMETER}</td>
126    <td>Hardware</td>
127    <td>Measures the acceleration force in m/s<sup>2</sup> that is applied to a device on
128all three physical axes (x, y, and z), including the force of gravity.</td>
129    <td>Motion detection (shake, tilt, etc.).</td>
130  </tr>
131  <tr>
132    <td>{@link android.hardware.Sensor#TYPE_AMBIENT_TEMPERATURE}</td>
133    <td>Hardware</td>
134    <td>Measures the ambient room temperature in degrees Celsius (&deg;C). See note below.</td>
135    <td>Monitoring air temperatures.</td>
136  <tr>
137    <td>{@link android.hardware.Sensor#TYPE_GRAVITY}</td>
138    <td>Software or Hardware</td>
139    <td>Measures the force of gravity in m/s<sup>2</sup> that is applied to a device on all
140      three physical axes (x, y, z).</td>
141    <td>Motion detection (shake, tilt, etc.).</td>
142   </tr>
143  <tr>
144    <td>{@link android.hardware.Sensor#TYPE_GYROSCOPE}</td>
145    <td>Hardware</td>
146    <td>Measures a device's rate of rotation in rad/s around each of the three
147physical axes
148      (x, y, and z).</td>
149    <td>Rotation detection (spin, turn, etc.).</td>
150   </tr>
151  <tr>
152    <td>{@link android.hardware.Sensor#TYPE_LIGHT}</td>
153    <td>Hardware</td>
154    <td>Measures the ambient light level (illumination) in lx.</td>
155    <td>Controlling screen brightness.</td>
156   </tr>
157  <tr>
158    <td>{@link android.hardware.Sensor#TYPE_LINEAR_ACCELERATION}</td>
159    <td>Software or Hardware</td>
160     <td>Measures the acceleration force in m/s<sup>2</sup> that is
161applied to a device on
162      all three physical axes (x, y, and z), excluding the force of gravity.</td>
163    <td>Monitoring acceleration along a single axis.</td>
164  </tr>
165  <tr>
166    <td>{@link android.hardware.Sensor#TYPE_MAGNETIC_FIELD}</td>
167    <td>Hardware</td>
168      <td>Measures the ambient geomagnetic field for all three physical axes (x, y, z) in
169&mu;T.</td>
170    <td>Creating a compass.</td>
171  </tr>
172  <tr>
173    <td>{@link android.hardware.Sensor#TYPE_ORIENTATION}</td>
174    <td>Software</td>
175     <td>Measures degrees of rotation that a device makes around all three physical axes (x, y, z).
176      As of API level 3 you can obtain the inclination matrix and rotation matrix for
177      a device by using the gravity sensor and the geomagnetic field sensor in conjunction with
178      the {@link android.hardware.SensorManager#getRotationMatrix getRotationMatrix()}
179      method.</td>
180    <td>Determining device position.</td>
181  </tr>
182  <tr>
183    <td>{@link android.hardware.Sensor#TYPE_PRESSURE}</td>
184    <td>Hardware</td>
185    <td>Measures the ambient air pressure in hPa or mbar.</td>
186    <td>Monitoring air pressure changes.</td>
187  </tr>
188  <tr>
189    <td>{@link android.hardware.Sensor#TYPE_PROXIMITY}</td>
190    <td>Hardware</td>
191    <td>Measures the proximity of an object in cm relative to the view screen of a
192      device. This sensor is typically used to determine whether a handset is being held up to
193      a person's ear.</td>
194    <td>Phone position during a call.</td>
195  </tr>
196  <tr>
197    <td>{@link android.hardware.Sensor#TYPE_RELATIVE_HUMIDITY}</td>
198    <td>Hardware</td>
199    <td>Measures the relative ambient humidity in percent (%).</td>
200    <td>Monitoring dewpoint, absolute, and relative humidity.</td>
201  </tr>
202  <tr>
203    <td>{@link android.hardware.Sensor#TYPE_ROTATION_VECTOR}</td>
204    <td>Software or Hardware</td>
205    <td>Measures the orientation of a device by providing the three elements of the device's
206      rotation vector.</td>
207    <td>Motion detection and rotation detection.</td>
208  </tr>
209  <tr>
210    <td>{@link android.hardware.Sensor#TYPE_TEMPERATURE}</td>
211    <td>Hardware</td>
212    <td>Measures the temperature of the device in degrees Celsius (&deg;C). This sensor
213implementation varies across devices and
214this sensor was replaced with the {@link android.hardware.Sensor#TYPE_AMBIENT_TEMPERATURE} sensor in
215API Level 14</td>
216    <td>Monitoring temperatures.</td>
217  </tr>
218</table>
219
220<h3>Sensor Framework</h3>
221
222<p>You can access these sensors and acquire raw sensor data by using the Android sensor framework.
223The sensor framework is part of the {@link android.hardware} package and includes the following
224classes and interfaces:</p>
225
226<dl>
227<dt>{@link android.hardware.SensorManager}</dt>
228<dd>You can use this class to create an instance of the sensor service. This class provides
229various methods for accessing and listing sensors, registering and unregistering sensor event
230listeners, and acquiring orientation information. This class also provides several sensor constants
231that are used to report sensor accuracy, set data acquisition rates, and calibrate sensors.</dd>
232<dt>{@link android.hardware.Sensor}</dt>
233<dd>You can use this class to create an instance of a specific sensor. This class provides various
234methods that let you determine a sensor's capabilities.</dd>
235<dt>{@link android.hardware.SensorEvent}</dt>
236<dd>The system uses this class to create a sensor event object, which provides information about a
237sensor event. A sensor event object includes the following information: the raw sensor data, the
238type of sensor that generated the event, the accuracy of the data, and the timestamp for the
239event.</dd>
240<dt>{@link android.hardware.SensorEventListener}</dt>
241<dd>You can use this interface to create two callback methods that receive notifications (sensor
242events) when sensor values change or when sensor accuracy changes.</dd>
243</dl>
244
245<p>In a typical application you use these sensor-related APIs to perform two basic tasks:</p>
246
247<ul>
248  <li><strong>Identifying sensors and sensor capabilities</strong>
249    <p>Identifying sensors and sensor capabilities at runtime is useful if your application has
250    features that rely on specific sensor types or capabilities. For example, you may want to
251    identify all of the sensors that are present on a device and disable any application features
252    that rely on sensors that are not present. Likewise, you may want to identify all of the sensors
253    of a given type so you can choose the sensor implementation that has the optimum performance
254    for your application.</p>
255  </li>
256  <li><strong>Monitor sensor events</strong>
257    <p>Monitoring sensor events is how you acquire raw sensor data. A sensor event occurs every time
258      a sensor detects a change in the parameters it is measuring. A sensor event provides you
259      with four pieces of information: the name of the sensor that triggered the event, the
260      timestamp for the event, the accuracy of the event, and the raw sensor data that triggered
261      the event.</p>
262  </li>
263</ul>
264
265<h3>Sensor Availability</h3>
266
267<p>While sensor availability varies from device to device, it can also vary between Android
268versions. This is because the Android sensors have been introduced over the course of several
269platform releases. For example, many sensors were introduced in Android 1.5 (API Level 3), but some
270were not implemented and were not available for use until Android 2.3 (API Level 9). Likewise,
271several sensors were introduced in Android 2.3 (API Level 9) and Android 4.0 (API Level 14). Two
272sensors have been deprecated and replaced by newer, better sensors.</p>
273
274<p>Table 2 summarizes the availability of each sensor on a platform-by-platform basis. Only four
275platforms are listed because those are the platforms that involved sensor changes. Sensors that are
276listed as deprecated are still available on subsequent platforms (provided the
277sensor is present on a device), which is in line with Android's forward compatibility policy.</p>
278
279<p class="table-caption" id="table2">
280  <strong>Table 2.</strong> Sensor availability by platform.</p>
281  <table>
282  <tr>
283    <th scope="col">Sensor</th>
284    <th scope="col">Android 4.0 <br>(API Level 14)</th>
285    <th scope="col">Android 2.3 <br>(API Level 9)</th>
286    <th scope="col">Android 2.2 <br>(API Level 8)</th>
287    <th scope="col">Android 1.5 <br>(API Level 3)</th>
288  </tr>
289  <tr>
290    <td>{@link android.hardware.Sensor#TYPE_ACCELEROMETER}</td>
291    <td><strong>Yes</strong></td>
292    <td><strong>Yes</strong></td>
293    <td><strong>Yes</strong></td>
294    <td><strong>Yes</strong></td>
295  </tr>
296  <tr>
297    <td>{@link android.hardware.Sensor#TYPE_AMBIENT_TEMPERATURE}</td>
298    <td><strong>Yes</strong></td>
299    <td>n/a</td>
300    <td>n/a</td>
301    <td>n/a</td>
302  </tr>
303  <tr>
304    <td>{@link android.hardware.Sensor#TYPE_GRAVITY}</td>
305    <td><strong>Yes</strong></td>
306    <td><strong>Yes</strong></td>
307    <td>n/a</td>
308    <td>n/a</td>
309  </tr>
310  <tr>
311    <td>{@link android.hardware.Sensor#TYPE_GYROSCOPE}</td>
312    <td><strong>Yes</strong></td>
313    <td><strong>Yes</strong></td>
314    <td>n/a<sup>1</sup></td>
315    <td>n/a<sup>1</sup></td>
316  </tr>
317  <tr>
318    <td>{@link android.hardware.Sensor#TYPE_LIGHT}</td>
319    <td><strong>Yes</strong></td>
320    <td><strong>Yes</strong></td>
321    <td><strong>Yes</strong></td>
322    <td><strong>Yes</strong></td>
323  </tr>
324  <tr>
325    <td>{@link android.hardware.Sensor#TYPE_LINEAR_ACCELERATION}</td>
326    <td><strong>Yes</strong></td>
327    <td><strong>Yes</strong></td>
328    <td>n/a</td>
329    <td>n/a</td>
330  </tr>
331  <tr>
332    <td>{@link android.hardware.Sensor#TYPE_MAGNETIC_FIELD}</td>
333    <td><strong>Yes</strong></td>
334    <td><strong>Yes</strong></td>
335    <td><strong>Yes</strong></td>
336    <td><strong>Yes</strong></td>
337  </tr>
338  <tr>
339    <td>{@link android.hardware.Sensor#TYPE_ORIENTATION}</td>
340    <td><strong>Yes</strong><sup>2</sup></td>
341    <td><strong>Yes</strong><sup>2</sup></td>
342    <td><strong>Yes</strong><sup>2</sup></td>
343    <td><strong>Yes</strong></td>
344  </tr>
345  <tr>
346    <td>{@link android.hardware.Sensor#TYPE_PRESSURE}</td>
347    <td><strong>Yes</strong></td>
348    <td><strong>Yes</strong></td>
349    <td>n/a<sup>1</sup></td>
350    <td>n/a<sup>1</sup></td>
351  </tr>
352  <tr>
353    <td>{@link android.hardware.Sensor#TYPE_PROXIMITY}</td>
354    <td><strong>Yes</strong></td>
355    <td><strong>Yes</strong></td>
356    <td><strong>Yes</strong></td>
357    <td><strong>Yes</strong></td>
358  </tr>
359  <tr>
360    <td>{@link android.hardware.Sensor#TYPE_RELATIVE_HUMIDITY}</td>
361    <td><strong>Yes</strong></td>
362    <td>n/a</td>
363    <td>n/a</td>
364    <td>n/a</td>
365  </tr>
366  <tr>
367    <td>{@link android.hardware.Sensor#TYPE_ROTATION_VECTOR}</td>
368    <td><strong>Yes</strong></td>
369    <td><strong>Yes</strong></td>
370    <td>n/a</td>
371    <td>n/a</td>
372  </tr>
373  <tr>
374    <td>{@link android.hardware.Sensor#TYPE_TEMPERATURE}</td>
375    <td><strong>Yes</strong><sup>2</sup></td>
376    <td><strong>Yes</strong></td>
377    <td><strong>Yes</strong></td>
378    <td><strong>Yes</strong></td>
379  </tr>
380</table>
381
382<p class="note"><strong><sup>1</sup></strong> This sensor type was added in Android 1.5 (API Level
3833),
384but it was not available for use until Android 2.3 (API Level 9).</p>
385
386<p class="note"><strong><sup>2</sup></strong> This sensor is available, but it has been
387deprecated.</p>
388
389<h2 id="sensors-identify">Identifying Sensors and Sensor Capabilities</h2>
390
391<p>The Android sensor framework provides several methods that make it easy for you to determine at
392runtime which sensors are on a device. The API also provides methods that let you determine the
393capabilities of each sensor, such as its maximum range, its resolution, and its power
394requirements.</p>
395
396<p>To identify the sensors that are on a device you first need to get a reference to the sensor
397service. To do this, you create an instance of the {@link android.hardware.SensorManager} class by
398calling the {@link android.content.Context#getSystemService getSystemService()} method and passing
399in the {@link android.content.Context#SENSOR_SERVICE SENSOR_SERVICE} argument. For example:</p>
400
401<pre>
402private SensorManager mSensorManager;
403...
404mSensorManager = (SensorManager) getSystemService(Context.SENSOR_SERVICE);
405</pre>
406
407<p>Next, you can get a listing of every sensor on a device by calling the
408{@link android.hardware.SensorManager#getSensorList getSensorList()} method and using the {@link
409android.hardware.Sensor#TYPE_ALL} constant. For example:</p>
410<pre>
411List&lt;Sensor&gt; deviceSensors = mSensorManager.getSensorList(Sensor.TYPE_ALL);
412</pre>
413
414<p>If you want to list all of the sensors of a given type, you could use another constant instead of
415{@link android.hardware.Sensor#TYPE_ALL} such as {@link android.hardware.Sensor#TYPE_GYROSCOPE},
416{@link android.hardware.Sensor#TYPE_LINEAR_ACCELERATION}, or
417{@link android.hardware.Sensor#TYPE_GRAVITY}.
418</p>
419
420<p>You can also determine whether a specific type of sensor exists on a device by using the {@link
421android.hardware.SensorManager#getDefaultSensor getDefaultSensor()} method and passing in the type
422constant for a specific sensor. If a device has more than one sensor of a given type, one of the
423sensors must be designated as the default sensor. If a default sensor does not exist for a given
424type of sensor, the method call returns null, which means the device does not have that type of
425sensor. For example, the following code checks whether there's a magnetometer on a device:</p>
426<pre>
427private SensorManager mSensorManager;
428...
429mSensorManager = (SensorManager) getSystemService(Context.SENSOR_SERVICE);
430if (mSensorManager.getDefaultSensor(Sensor.TYPE_MAGNETIC_FIELD) != null){
431  // Success! There's a magnetometer.
432  }
433else {
434  // Failure! No magnetometer.
435  }
436</pre>
437
438<p class="note"><strong>Note:</strong> Android does not require device manufacturers to build any
439particular types of sensors into their Android-powered devices, so devices can have a wide range of
440sensor configurations.</p>
441
442<p>In addition to listing the sensors that are on a device, you can use the public methods of the
443{@link android.hardware.Sensor} class to determine the capabilities and attributes of individual
444sensors. This is useful if you want your application to behave differently based on which sensors or
445sensor capabilities are available on a device. For example, you can use the {@link
446android.hardware.Sensor#getResolution} and {@link android.hardware.Sensor#getMaximumRange}
447methods to obtain a sensor's resolution and maximum range of measurement. You can also use the
448{@link android.hardware.Sensor#getPower} method to obtain a sensor's power requirements.</p>
449
450<p>Two of the public methods are particularly useful if you want to optimize your application for
451different manufacturer's sensors or different versions of a sensor. For example, if your application
452needs to monitor user gestures such as tilt and shake, you could create one set of data filtering
453rules and optimizations for newer devices that have a specific vendor's gravity sensor, and another
454set of data filtering rules and optimizations for devices that do not have a gravity sensor and have
455only an accelerometer. The following code sample shows you how you can use the {@link
456android.hardware.Sensor#getVendor} and {@link android.hardware.Sensor#getVersion} methods to do
457this. In this sample, we're looking for a gravity sensor that lists Google Inc. as the vendor and
458has a version number of 3. If that particular sensor is not present on the device, we try to use the
459accelerometer.</p>
460
461<pre>
462private SensorManager mSensorManager;
463private Sensor mSensor;
464
465...
466
467mSensorManager = (SensorManager) getSystemService(Context.SENSOR_SERVICE);
468mSensor = null;
469
470if (mSensorManager.getDefaultSensor(Sensor.TYPE_GRAVITY) != null){
471  List&lt;Sensor&gt; gravSensors = mSensorManager.getSensorList(Sensor.TYPE_GRAVITY);
472  for(int i=0; i&lt;gravSensors.size(); i++) {
473    if ((gravSensors.get(i).getVendor().contains("Google Inc.")) &&
474       (gravSensors.get(i).getVersion() == 3)){
475      // Use the version 3 gravity sensor.
476      mSensor = gravSensors.get(i);
477    }
478  }
479}
480if (mSensor == null){
481  // Use the accelerometer.
482  if (mSensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER) != null){
483    mSensor = mSensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);
484  }
485  else{
486    // Sorry, there are no accelerometers on your device.
487    // You can't play this game.
488  }
489}
490</pre>
491
492<p>Another useful method is the {@link android.hardware.Sensor#getMinDelay getMinDelay()} method,
493which returns the minimum time interval (in microseconds) a sensor can use to sense data. Any sensor
494that returns a non-zero value for the {@link android.hardware.Sensor#getMinDelay getMinDelay()}
495method is a streaming
496sensor. Streaming sensors sense data at regular intervals and were introduced in Android 2.3 (API
497Level 9). If a sensor returns zero when you call the {@link android.hardware.Sensor#getMinDelay
498getMinDelay()} method, it means the
499sensor is not a streaming sensor because it reports data only when there is a change in the
500parameters it is sensing.</p>
501
502<p>The {@link android.hardware.Sensor#getMinDelay getMinDelay()} method is useful because it lets
503you determine the maximum rate
504at which a sensor can acquire data. If certain features in your application require high data
505acquisition rates or a streaming sensor, you can use this method to determine whether a sensor
506meets those requirements and then enable or disable the relevant features in your application
507accordingly.</p>
508
509<p class="caution"><strong>Caution:</strong> A sensor's maximum data acquisition rate is not
510necessarily the rate at which the sensor framework delivers sensor data to your application. The
511sensor framework reports data through sensor events, and several factors influence the rate at which
512your application receives sensor events. For more information, see <a
513href="#sensors-monitor">Monitoring Sensor Events</a>.</p>
514
515<h2 id="sensors-monitor">Monitoring Sensor Events</h2>
516
517<p>To monitor raw sensor data you need to implement two callback methods that are exposed through
518the {@link android.hardware.SensorEventListener} interface: {@link
519android.hardware.SensorEventListener#onAccuracyChanged onAccuracyChanged()} and {@link
520android.hardware.SensorEventListener#onSensorChanged onSensorChanged()}. The Android system calls
521these methods whenever the following occurs:</p>
522
523<ul>
524  <li><strong>A sensor's accuracy changes.</strong>
525    <p>In this case the system invokes the {@link
526android.hardware.SensorEventListener#onAccuracyChanged onAccuracyChanged()} method, providing
527    you with a reference to the {@link android.hardware.Sensor Sensor} object that changed and the
528    new accuracy of the sensor. Accuracy is represented by one of four status constants:
529    {@link android.hardware.SensorManager#SENSOR_STATUS_ACCURACY_LOW},
530    {@link android.hardware.SensorManager#SENSOR_STATUS_ACCURACY_MEDIUM},
531    {@link android.hardware.SensorManager#SENSOR_STATUS_ACCURACY_HIGH},
532    or {@link android.hardware.SensorManager#SENSOR_STATUS_UNRELIABLE}.</p>
533  </li>
534  <li><strong>A sensor reports a new value.</strong>
535    <p>In this case the system invokes the {@link
536android.hardware.SensorEventListener#onSensorChanged onSensorChanged()} method, providing you with
537a {@link android.hardware.SensorEvent SensorEvent} object. A {@link android.hardware.SensorEvent
538SensorEvent} object
539      contains information about the new sensor data, including: the accuracy of the data, the
540      sensor that generated the data, the timestamp at which the data was generated, and the new
541      data that the sensor recorded.</p>
542  </li>
543</ul>
544
545<p>The following code shows how to use the {@link
546android.hardware.SensorEventListener#onSensorChanged onSensorChanged()} method to monitor data from
547the light sensor. This example displays the raw sensor data in a {@link android.widget.TextView}
548that is
549defined in the main.xml file as <code>sensor_data</code>.</p>
550
551<pre>
552public class SensorActivity extends Activity implements SensorEventListener {
553  private SensorManager mSensorManager;
554  private Sensor mLight;
555
556  &#64;Override
557  public final void onCreate(Bundle savedInstanceState) {
558    super.onCreate(savedInstanceState);
559    setContentView(R.layout.main);
560
561    mSensorManager = (SensorManager) getSystemService(Context.SENSOR_SERVICE);
562    mLight = mSensorManager.getDefaultSensor(Sensor.TYPE_LIGHT);
563  }
564
565  &#64;Override
566  public final void onAccuracyChanged(Sensor sensor, int accuracy) {
567    // Do something here if sensor accuracy changes.
568  }
569
570  &#64;Override
571  public final void onSensorChanged(SensorEvent event) {
572    // The light sensor returns a single value.
573    // Many sensors return 3 values, one for each axis.
574    float lux = event.values[0];
575    // Do something with this sensor value.
576  }
577
578  &#64;Override
579  protected void onResume() {
580    super.onResume();
581    mSensorManager.registerListener(this, mLight, SensorManager.SENSOR_DELAY_NORMAL);
582  }
583
584  &#64;Override
585  protected void onPause() {
586    super.onPause();
587    mSensorManager.unregisterListener(this);
588  }
589}
590</pre>
591
592<p>In this example, the default data delay ({@link
593android.hardware.SensorManager#SENSOR_DELAY_NORMAL}) is specified when the {@link
594android.hardware.SensorManager#registerListener registerListener()} method is invoked. The data
595delay (or sampling rate) controls the interval at which sensor events are sent to your application
596via the {@link
597android.hardware.SensorEventListener#onSensorChanged onSensorChanged()} callback method. The default
598data delay is suitable for monitoring
599typical screen orientation changes and uses a delay of 200,000 microseconds. You can specify other
600data delays, such as {@link android.hardware.SensorManager#SENSOR_DELAY_GAME} (20,000 microsecond
601delay), {@link android.hardware.SensorManager#SENSOR_DELAY_UI} (60,000 microsecond delay), or {@link
602android.hardware.SensorManager#SENSOR_DELAY_FASTEST} (0 microsecond delay). As of Android 3.0 (API
603Level 11) you can also specify the delay as an absolute value (in microseconds).</p>
604
605<p>The delay that you specify is only a suggested delay. The Android system and other applications
606can alter this delay. As a best practice, you should specify the largest delay that you can because
607the system typically uses a smaller delay than the one you specify (that is, you should choose the
608slowest sampling rate that still meets the needs of your application). Using a larger delay imposes
609a lower load on the processor and therefore uses less power.</p>
610
611<p>There is no public method for determining the rate at which the sensor framework is sending
612sensor events to your application; however, you can use the timestamps that are associated with each
613sensor event to calculate the sampling rate over several events. You should not have to change the
614sampling rate (delay) once you set it. If for some reason you do need to change the delay, you will
615have to unregister and reregister the sensor listener.</p>
616
617<p>It's also important to note that this example uses the {@link android.app.Activity#onResume} and
618{@link android.app.Activity#onPause} callback methods to register and unregister the sensor event
619listener. As a best practice you should always disable sensors you don't need, especially when your
620activity is paused. Failing to do so can drain the battery in just a few hours because some sensors
621have substantial power requirements and can use up battery power quickly. The system
622will not disable sensors automatically when the screen turns off.</p>
623
624<h2 id="sensors-configs">Handling Different Sensor Configurations</h2>
625
626<p>Android does not specify a standard sensor configuration for devices,
627which means device manufacturers can incorporate any sensor configuration that they want into their
628Android-powered devices. As a result, devices can include a variety
629of sensors in a wide range of configurations. For example, the Motorola Xoom has a pressure sensor,
630but the Samsung Nexus S does not. Likewise, the Xoom and Nexus S have gyroscopes, but the HTC Nexus
631One does not. If your application relies on a specific type of sensor, you have to ensure that the
632sensor is present on a device so your app can run successfully. You have two options for ensuring
633that a given sensor is present on a device:</p>
634<ul>
635  <li>Detect sensors at runtime and enable or disable application features as appropriate.</li>
636  <li>Use Google Play filters to target devices with specific sensor configurations.</li>
637</ul>
638
639<p>Each option is discussed in the following sections.</p>
640
641<h4><strong>Detecting sensors at runtime</strong></h4>
642
643<p>If your application uses a specific type of sensor, but doesn't rely on it, you can use the
644sensor framework to detect the sensor at runtime and then disable or enable application features
645as appropriate. For example, a navigation application might use the temperature sensor,
646pressure sensor, GPS sensor, and geomagnetic field sensor to display the temperature, barometric
647pressure, location, and compass bearing. If a device doesn't have a pressure sensor, you can use the
648sensor framework to detect the absence of the pressure sensor at runtime and then disable the
649portion of your application's UI that displays pressure. For example, the following code checks
650whether there's a pressure sensor on a device:</p>
651<pre>
652  private SensorManager mSensorManager;
653  ...
654  mSensorManager = (SensorManager) getSystemService(Context.SENSOR_SERVICE);
655  if (mSensorManager.getDefaultSensor(Sensor.TYPE_PRESSURE) != null){
656  // Success! There's a pressure sensor.
657  }
658  else {
659  // Failure! No pressure sensor.
660  }
661</pre>
662
663<h4>Using Google Play filters to target specific sensor configurations</h4>
664
665<p>If you are publishing your application on Google Play you can use the
666  <a href="{@docRoot}guide/topics/manifest/uses-feature-element.html"><code>&lt;uses-feature&gt;
667    </code></a> element in your manifest file to filter your application from devices that do not
668have the appropriate sensor configuration for your application. The
669<code>&lt;uses-feature&gt;</code> element has several hardware descriptors that let you filter
670applications based on the presence of specific sensors. The sensors you can list include:
671accelerometer, barometer, compass (geomagnetic field), gyroscope, light, and proximity. The
672following is an example manifest entry that filters apps that do not have an accelerometer:</p>
673
674<pre>
675&lt;uses-feature android:name="android.hardware.sensor.accelerometer"
676              android:required="true" /&gt;
677</pre>
678
679<p>If you add this element and descriptor to your application's manifest, users will see your
680application on Google Play only if their device has an accelerometer.</p>
681
682<p>You should set the descriptor to <code>android:required="true"</code> only if your application
683relies entirely on a specific sensor. If your application uses a sensor for some functionality, but
684still runs without the sensor, you should list the sensor in the <code>&lt;uses-feature&gt;</code>
685element, but set the descriptor to <code>android:required="false"</code>. This helps ensure that
686devices can install your app even if they do not have that particular sensor. This is also a
687project management best practice that helps you keep track of the features your application uses.
688Keep in mind, if your application uses a particular sensor, but still runs without the sensor,
689then you should detect the sensor at runtime and disable or enable application features as
690appropriate.</p>
691
692<h2 id="sensors-coords">Sensor Coordinate System</h2>
693
694<p>In general, the sensor framework uses a standard 3-axis coordinate system to express data values.
695For most sensors, the coordinate system is defined relative to the device's screen when the device
696is held in its default orientation (see figure 1). When a device is held in its default orientation,
697the X axis is horizontal and points to the right, the Y axis is vertical and points up, and the Z
698axis points toward the outside of the screen face. In this system, coordinates behind the screen
699have negative Z values. This coordinate system is used by the following sensors:</p>
700
701<div class="figure" style="width:269px">
702  <img src="{@docRoot}images/axis_device.png" alt="" height="225" />
703  <p class="img-caption">
704    <strong>Figure 1.</strong> Coordinate system (relative to a device) that's used by the Sensor
705    API.
706  </p>
707</div>
708
709<ul>
710  <li><a
711href="{@docRoot}guide/topics/sensors/sensors_motion.html#sensors-motion-accel">Acceleration
712sensor</a></li>
713<li><a
714href="{@docRoot}guide/topics/sensors/sensors_motion.html#sensors-motion-grav">Gravity
715sensor</a></li>
716<li><a
717href="{@docRoot}guide/topics/sensors/sensors_motion.html#sensors-motion-gyro">Gyroscope</a></li>
718<li><a
719href="{@docRoot}guide/topics/sensors/sensors_motion.html#sensors-motion-linear">Linear acceleration
720sensor</a></li>
721<li><a
722href="{@docRoot}guide/topics/sensors/sensors_position.html#sensors-pos-mag">Geomagnetic field
723sensor</a></li>
724</ul>
725
726<p>The most important point to understand about this coordinate system is that the axes are not
727swapped when the device's screen orientation changes&mdash;that is, the sensor's coordinate system
728never changes as the device moves. This behavior is the same as the behavior of the OpenGL
729coordinate system.</p>
730
731<p>Another point to understand is that your application must not assume that a device's natural
732(default) orientation is portrait. The natural orientation for many tablet devices is landscape. And
733the sensor coordinate system is always based on the natural orientation of a device.</p>
734
735<p>Finally, if your application matches sensor data to the on-screen display, you need to use the
736{@link android.view.Display#getRotation} method to determine screen rotation, and then use the
737{@link android.hardware.SensorManager#remapCoordinateSystem remapCoordinateSystem()} method to map
738sensor coordinates to screen coordinates. You need to do this even if your manifest specifies
739portrait-only display.</p>
740
741<p>For more information about the sensor coordinate system, including information about how to
742handle screen rotations, see <a
743href="http://android-developers.blogspot.com/2010/09/one-screen-turn-deserves-another.html">One
744Screen Turn Deserves Another</a>.</p>
745
746<p class="note"><strong>Note:</strong> Some sensors and methods use a coordinate system that is
747relative to the world's frame of reference (as opposed to the device's frame of reference). These
748sensors and methods return data that represent device motion or device position relative to the
749earth. For more information, see the {@link android.hardware.SensorManager#getOrientation
750getOrientation()} method, the {@link android.hardware.SensorManager#getRotationMatrix
751getRotationMatrix()} method, <a
752href="{@docRoot}guide/topics/sensors/sensors_position.html#sensors-pos-orient">Orientation
753Sensor</a>, and <a
754href="{@docRoot}guide/topics/sensors/sensors_motion.html#sensors-motion-rotate">Rotation Vector
755Sensor</a>.</p>
756
757<h2 id="sensors-practices">Best Practices for Accessing and Using Sensors</h2>
758
759<p>As you design your sensor implementation, be sure to follow the guidelines that are discussed in
760this section. These guidelines are recommended best practices for anyone who is using the sensor
761framework to access sensors and acquire sensor data.</p>
762
763<h4>Unregister sensor listeners</h4>
764
765<p>Be sure to unregister a sensor's listener when you are done using the sensor or when the sensor
766activity pauses. If a sensor listener is registered and its activity is paused, the sensor will
767continue to acquire data and use battery resources unless you unregister the sensor. The following
768code shows how to use the {@link android.app.Activity#onPause} method to unregister a listener:</p>
769
770<pre>
771private SensorManager mSensorManager;
772  ...
773&#64;Override
774protected void onPause() {
775  super.onPause();
776  mSensorManager.unregisterListener(this);
777}
778</pre>
779
780<p>For more information, see {@link android.hardware.SensorManager#unregisterListener}.</p>
781
782<h4>Don't test your code on the emulator</h4>
783
784<p>You currently can't test sensor code on the emulator because the emulator cannot emulate sensors.
785You must test your sensor code on a physical device. There are, however, sensor simulators that you
786can use to simulate sensor output.</p>
787
788<h4>Don't block the onSensorChanged() method</h4>
789
790<p>Sensor data can change at a high rate, which means the system may call the {@link
791android.hardware.SensorEventListener#onSensorChanged} method quite often. As a best practice, you
792should do as little as possible within the {@link
793android.hardware.SensorEventListener#onSensorChanged} method so you don't block it. If your
794application requires you to do any data filtering or reduction of sensor data, you should perform
795that work outside of the {@link android.hardware.SensorEventListener#onSensorChanged} method.</p>
796
797<h4>Avoid using deprecated methods or sensor types</h4>
798
799<p>Several methods and constants have been deprecated.
800In particular, the {@link android.hardware.Sensor#TYPE_ORIENTATION}
801sensor type has been deprecated. To get orientation data you should use the {@link
802android.hardware.SensorManager#getOrientation getOrientation()} method instead. Likewise, the
803{@link android.hardware.Sensor#TYPE_TEMPERATURE} sensor type has been deprecated. You should use
804the {@link android.hardware.Sensor#TYPE_AMBIENT_TEMPERATURE} sensor type instead on devices
805that are running Android 4.0.</p>
806
807<h4>Verify sensors before you use them</h4>
808
809<p>Always verify that a sensor exists on a device before you attempt to acquire data from it. Don't
810assume that a sensor exists simply because it's a frequently-used sensor. Device manufacturers are
811not required to provide any particular sensors in their devices.</p>
812
813<h4>Choose sensor delays carefully</h4>
814
815<p>When you register a sensor with the {@link android.hardware.SensorManager#registerListener
816registerListener()} method, be sure you choose a delivery rate that is suitable for your
817application or use-case. Sensors can provide data at very high rates. Allowing the system to send
818extra data that you don't need wastes system resources and uses battery power.</p>