AndroidHTP 11 IM File

11
Route Tracker App
Objectives
In this chapter you’ll:
■
Test an app that uses GPS
location data in the Android
Emulator and use the Eclipse
DDMS perspective to send
sample GPS data to the
emulator.
■
Use the external Maps API
framework and the
MapActivity and
MapView classes to display
Google Maps™ generated by
Google web services.
■
Get a Google Maps™ API key
unique to your development
computer.
■
Use location services and the
LocationManager class to
receive information on the
device’s position and bearing
(direction).
■
Display the user’s route using
an Overlay on a MapView
and GPS location data
received in the form of
Location objects.
■
Orient a map to the user’s
current bearing.
■
Use the PowerManager to
keep the device awake.
Self-Review Exercisess
2
Self-Review Exercisess
11.1
Fill in the blanks in each of the following statements:
provides access to the device’s location services.
a) Class
ANS: LocationManager.
b) To determine when the device has a GPS fix—that is, the device has “locked onto”
enough GPS satellites to receive GPS data for tracking—we implement the
interface.
ANS: GpsStatus.Listener.
(package android.view) provide’s access to the device’s screen dic) Class
mensions.
ANS: Display.
: indicates that this app requires Internet access to downd) The permission
load map and satellite images.
ANS: android.permission.INTERNET.
to programmatically zoom in and out of a map and to change
e) You use a
the geographic location appearing centered in the MapView.
ANS: MapController
f)
MapController’s
method moves the center of the map to the given
using a smooth animation.
ANS: animateTo.
method moves the center of the map to the given
g) MapController’s
Point using a smooth animation.
ANS: animateTo.
Geo-
Point
11.2
Geo-
State whether each of the following is true or false. If false, explain why.
a) (True/False) To create your own app using the Google Maps API, you’ll need to obtain
a unique API key from Google.
ANS: True.
b) (True/False) If you’d like your app to use most of the screen to display maps, you can
hide the title bar by using one of the standard Android themes, which can be specified
with the attribute android:theme in the activity element. A theme changes the lookand-feel of an app’s GUI.
ANS: True.
c) (True/False) Shared Android services include those that allow an app to change power
settings, obtain location data, control whether a device is allowed to sleep, and more.
ANS: True.
d) (True/False) MapViews support gestures to zoom and pan the map—any additional functionality must be added programmatically.
ANS: True.
e) (True/False) LocationManager’s getBestProvider method returns a String representing the name of the location provider that best meets the given Criteria. The true argument indicates that only an enabled provider should be returned.
ANS: True.
f) (True/False) Class PowerManager enables the app to control a device’s power state so
that, for example, the app can record location data even if the screen is off.
ANS: True.
3
Chapter 11
Route Tracker App
Exercises
11.3
Fill in the blanks in each of the following statements:
a) To access a nonstandard library—that is, one that’s not included with the core Android
APIs, such as the Google Maps API—you must indicate the library’s name in the app’s
element nested in the application element.
manifest with a
ANS: uses-library.
b) To access shared Android services, you must request permission to use them in the manelements nested in the root manifest element. When a user
ifest file with
prepares to install an app, the operating system tells the user which permissions are being requested and asks the user to confirm whether they should be granted. If not, the
app will not be installed.
ANS: uses-permission.
c) To display data on a MapView, such as the line representing the route in this app, you
and override its draw method.
create a subclass of
ANS: Overlay.
d) Once you have a location provider, you can request updates from it and have them delivered to a LocationListener. The updates are delivered to the listener as
objects that represent the device’s geographic location—these include latitude and longitude data, the time they were recorded and, depending on the location provider, may
also include altitude and speed data (some devices don’t have sensors for these).
ANS: Location.
method to get the location
e) Many GPS-based apps use LocationManager’s
that was last reported when the device previously had a GPS fix (such as during a previous execution of the app).
ANS: getLastKnownLocation.
f) Class LocationManager provides access to the device’s location services and chooses the
object.
best location provider based on the requirements you specify in a
To determine when the device has a GPS fix, you implement the GpsStatus.Listener
interface.
ANS: Criteria.
11.4
State whether each of the following is true or false. If false, explain why.
a) (True/False) Apps must be signed with a digital certificate before they can be installed
on a device. When you’re building and testing apps, the ADT Plugin handles this automatically by creating a debug certificate and using it to sign your apps.
ANS: True.
b) (True/False) Depending on your device, several location providers may be supported—
LocationManager provides capabilities for choosing the best one based on your app’s requirements, which you specify in a Requirements object.
ANS: False. Depending on your device, several location providers may be supported—LocationManager provides capabilities for choosing the best one based on your app’s requirements, which you specify in a Criteria object.
c) (True/False) An app that changes the power settings can negatively affect the device’s
battery life when the app is executing, so class PowerManager should be used frequently.
ANS: False. An app that changes the power settings can negatively affect the device’s battery
life when the app is executing, so class PowerManager should be used sparingly.
d) (True/False) The permission android.permission.WAKE_LOCK indicates that this app
needs access to the PowerManager to prevent the device from sleeping while the app is
tracking a route.
ANS: True.
Exercises
4
e) (True/False) High-accuracy GPS data uses less power. If your app doesn’t require such
accuracy, you can choose Criteria.ACCURACY_COARSE.
ANS: False. High-accuracy GPS data uses more power. If your app doesn’t require such accuracy, you can choose Criteria.ACCURACY_COARSE.
f) (True/False) A GeoPoint consists of a latitude and longitude measured in degrees.
ANS: False. A GeoPoint consists of a latitude and longitude measured in microdegrees (millionths of a degree).