Sensors and Location Services in Android Development

sensors and location n.w
1 / 22
Embed
Share

Discover the world of sensors and location services in Android development. Learn about available sensors like Accelerometer, Gyroscope, Magnetometer, and more. Explore how to interface with the SensorManager and LocationManager, register for sensor updates, and implement SensorEventListeners. Master the coordinate system and utilize sensor data effectively in your Android apps.

  • Android Development
  • Sensors
  • Location Services
  • SensorManager
  • SensorEventListener

Uploaded on | 0 Views


Download Presentation

Please find below an Image/Link to download the presentation.

The content on the website is provided AS IS for your information and personal use only. It may not be sold, licensed, or shared on other websites without obtaining consent from the author. If you encounter any issues during the download, it is possible that the publisher has removed the file from their server.

You are allowed to download the files provided on this website for personal or commercial use, subject to the condition that they are used lawfully. All files are the property of their respective owners.

The content on the website is provided AS IS for your information and personal use only. It may not be sold, licensed, or shared on other websites without obtaining consent from the author.

E N D

Presentation Transcript


  1. SENSORS AND LOCATION

  2. ANDROID SENSORS AND LOCATIONS

  3. Sensors Available sensors Accelerometer Gyroscope Magnetometer Proximity Temperature Light Microphone Camera GPS

  4. SensorManager Most sensors interfaced through SensorManager or LocationManager Obtain pointer to android service using Context.getSystemService( name ) For name, use constant defined by Context class SENSOR_SERVICE for SensorManager LOCATION_SERVICE for LocationManager Check for available sensors using List<Sensor> getSensorList(int type) Type constants provided in Sensor class documentation

  5. SensorManager Use getDefaultSensor(int type) to get a pointer to the default sensor for a particular type Sensor accel = sensorManager.getDefaultSensor ( Sensor.TYPE_ACCELEROMETER ); Register for updates of sensor values using registerListener (SensorEventListener, Sensor, rate) rate is an int, using one of the following 4 constants SENSOR_DELAY_NORMAL SENSOR_DELAY_UI SENSOR_DELAY_GAME SENSOR_DELAY_FASTEST Use the lowest rate necessary to reduce power usage

  6. SensorManager Unregister for sensor events using unregisterListener (SensorEventListener, Sensor) or unregisterListener (SensorEventListener) Perform register in OnResume() and unregister in OnPause() to prevent using resources while your activity is not visible SensorListener is depracated, use SensorEventListener instead See documentation for Sensor, SensorManager, SensorEvent and SensorEventListener

  7. SensorEventListener Must implement two methods onAccuracyChanged (Sensor sensor, int accuracy) onSensorChanged (SensorEvent event) SensorEvent int accuracy Sensor sensor long timestamp Time in nanoseconds at which event happened float[] values Length and content of values depends on sensor type

  8. Coordinate system

  9. Sensors Some sensor types are raw values of physical sensors, others are derived from sensors Example raw types TYPE_ACCELEROMETER TYPE_GYROSCOPE Example derived values TYPE_LINEAR_ACCELERATION TYPE_ORIENTATION Some types depend on the underlying hardware, e.g., TYPE_PROXIMITY may be derived from ambient light sensor

  10. Location Providers GPS_PROVIDER GPS NETWORK_PROVIDER Wifi Cellular PASSIVE_PROVIDER Piggy-back on other updates Each provider has trade-off of power usage, delay, and accuracy Use minimum accuracy necessary for application to reduce power usage - meters - 100 meters - 1 km

  11. LocationManager Register for location updates using void requestLocationUpdates (. . .) Multiple options for request arguments, based on type of request needed. See documentation for all options. Typically pass a LocationListener (like SensorEventListener)

  12. LocationListener Methods onLocationChanged (Location location) onProviderDisabled (String provider) onProviderEnabled (String provider) onStatusChanged (String Provider, int status, Bundle extras) Provider is the technology used for location measurements

  13. Request permissions Register permission in manifest file android.permission.ACCESS_FINE_LOCATION If (possibly) using GPS android.permission.ACCESS_COARSE_LOCATION For any other location access <manifest ... > <uses-permission android:name= "android.permission.ACCESS_FINE_LOCATION" /> ... </manifest>

  14. Energy usage Reduce power usage wherever possible Unregister listener when new updates are not needed removeUpdates(LocationListener) Reduce frequency of updates Use higher threshold values for minimum distance and time interval Restrict providers used Use lower cost providers when possible

  15. MapView Interface to Google Maps API Maps external library not part of standard Android library Install Google API in SDK and AVD Manager Select Google API as target for project Must register for Google Maps API key http://code.google.com/android/add-ons/google- apis/mapkey.html MapActivityprovides subclass of Activity linked to MapView (similar to ListActivity)

  16. Request permissions Add permission and library requests in manifest file Include location permission as before android.permission.INTERNET To interface Google Maps API com.google.android.maps For Google maps library <manifest ... > <uses-permission android:name="android.permission.INTERNET"/> <application ... > <uses-library android:name="com.google.android.maps" /> ... </application> ... </manifest>

  17. Map API key Add Google Map API key to MapView In layout xml file, add following attribute to MapViewitem <com.google.android.maps.MapView . . . android:apiKey= your key here />

  18. MapView class Enable and configure zoom controls Obtain MapController for control of map Obtain overlay list to add new overlay Set map mode Satellite Street view Traffic

  19. MapController class Animate/pan map Zoom In Out Set Re-center map

  20. GeoPoint class A latitiude/longitude position stored as integer numbers in microdegrees degrees 1E6

  21. Overlay class Add overlay images or icons at GeoPoints Abstract class, use MyLocationOverlay or subclass ItemizedOverlay MyLocationOverlay Automatically provide location tracking and indicate position with blue dot ItemizedOverlay Subclass to provide a list of overlay icons Can implement touch events

  22. Testing Limited simulation support for sensors To test Android application on device, add debuggable attribute to manifest file <manifest ... > <application ... android:debuggable="true" > ... </application> ... </manifest>

More Related Content