Download presentation
Presentation is loading. Please wait.
1
Tracking device movements
2
In this chapter Android location service to continuously track device location Google Maps library to plot location data on a map Broadcast receivers to track location in the background Considering effects on battery life
3
The example app will shows:
how to receive the device’s current location, persist that location in a database, and plot the path traveled places on a map using the Google Maps external library for Android.
4
Additional functionality comes with additional complexities
keeping more device hardware adverse affect battery life incorrect location data
5
Three Android application components:
An activity to display both the current location and previous locations A broadcast receiver with location data in the background and stores the new locations in a database Another broadcast receiver that receives location updates only when the app is in the foreground in order to update the display
7
The app should continue to collect and save location data even when not in the foreground.
background tasks are services and broadcast receivers that can do this The app uses a broadcast receiver A receiver is passed intents based on a filter, and those intents contain data for the broadcast receiver to read and process.
8
Create a class that extends BroadcastReceiver.
Register the child class as a BroadcastReceiver with Android. Register an intent to be broadcast when Android receives new location information with the LocationManager.
11
To have a broadcast receiver receive intents that were broadcast, it needs to be registered with Android. Two ways to perform the registration: in the manifest for your app or by calling registerReceiver() on a Context.
13
Once the broadcast receiver has been implemented and registered with Android, the app needs to request that an intent is broadcast when location data updates are available. Before this request is made, a PendingIntent needs to be created.
14
Once the pending intent is created, location updates are requested by calling LocationManager. requestLocationUpdates() to register a LocationListener.
15
Both TrackLocationBroadcastReceiver and UpdateViewBroadcastReceiver instead extend LocationBroadcastReceiver.
16
Services are components for performing tasks in the background in Android.
17
One reason to choose a broadcast receiver over a service for certain background tasks is that they can be a lighter weight application component for passively collecting location data compared to a service. In order to present the location data to the user, the TrackLocationActivity will display a Google map with the tracked points plotted on it.
18
TrackLocationActivity uses the following classes from the Google Maps external library:
MapView OverlayItem ItemizedOverlay MapActivity
19
The MapView is the view that displays the map in TrackLocationActivity.
OverlayItem is an object that is drawn on the map. The overlay item is a container for the location data — latitude, longitude, and accuracy — that needs to be represented on the map.
20
ItemizedOverlay holds the list of overlay items that need to be drawn on the overlay, and defines how to draw the items. The draw() method defines how each overlay item will be drawn. MapActivity adds two methods that deserve some special attention: isRouteDisplayed() and isLocationDisplayed(). used by the Google Maps library for accounting purposes and need to reflect if the activity is currently displaying route and location information, respectively
23
Location data filtering is used when multiple location providers may be providing location data simultaneously. The example app provides the filtering algorithms in the FilteringLocationBroadcastReceiver class.
25
To reduce battery consumption reduce the frequency at which location updates need to be acquired from the location services. By configuring the parameters passed to LocationManager.requestLocationUpdates(). locationManager.requestLocationUpdates(provider, 0, 0, pendingIntent);. The second and third parameters define minimum time and minimum distance at which location updates should be received.
26
Another way to improve battery life is to limit the location providers that are used to acquire location updates. Although the user may have every location provider enabled, an app does not need to request updates from all of them.
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.