The following is the first few sections of a chapter from The Busy Coder's Guide to Android Development, plus headings for the remaining major sections, to give you an idea about the content of the chapter.


Accessing Location-Based Services

A popular feature on current-era mobile devices is GPS capability, so the device can tell you where you are at any point in time. While the most popular use of GPS service is mapping and directions, there are other things you can do if you know your location. For example, you might set up a dynamic chat application where the people you can chat with are based on physical location, so you are chatting with those you are nearest. Or, you could automatically “geotag” posts to Twitter or similar services.

GPS is not the only way a mobile device can identify your location. Alternatives include:

  1. Cell tower triangulation, where your position is determined based on signal strength to nearby cell towers
  2. Proximity to public WiFi “hotspots” that have known geographic locations
  3. GPS alternatives, such as GLONASS (Russia), Galileo (European Union, still under development), and Compass (China, still under development)

Android devices may have one or more of these services available to them. You, as a developer, can ask the device for your location, plus details on what providers are available. There are even ways for you to simulate your location in the emulator, for use in testing your location-enabled applications.

Prerequisites

Understanding this chapter requires that you have read the core chapters, particularly the chapter on threads.

Location Providers: They Know Where You’re Hiding

Android devices can have access to several different means of determining your location. Some will have better accuracy than others. Some may be free, while others may have a cost associated with them. Some may be able to tell you more than just your current position, such as your elevation over sea level, or your current speed.

Android, therefore, has abstracted all this out into a set of LocationProvider objects. Your Android environment will have zero or more LocationProvider instances, one for each distinct locating service that is available on the device. Providers know not only your location, but also their own characteristics, in terms of accuracy, cost, etc. There are two main providers: GPS_PROVIDER (which uses GPS) and NETWORK_PROVIDER (which uses cell tower triangulation and WiFi hotspot proximity).

You, as a developer, will use a LocationManager, which holds the LocationProvider set, to figure out which LocationProvider is right for your particular circumstance. You will also need a permission in your application, or the various location APIs will fail due to a security violation.

Depending on which location providers you wish to use, you may need ACCESS_COARSE_LOCATION or ACCESS_FINE_LOCATION. Note that ACCESS_COARSE_LOCATION may intentionally “fuzz” or filter out location fixes that are “too good” (i.e., more accurate than a city block), such as those obtained from being near a known WiFi hotspot. The GPS_PROVIDER specifically requires ACCESS_FINE_LOCATION to work, at least on modern versions of Android. Also note that these permissions are dangerous, and therefore if your targetSdkVersion is 23 or higher, you need to ask for these permissions at runtime.

Finding Yourself

The preview of this section was eaten by a grue.

On the Move

The preview of this section was stepped on by Godzilla.

Getting Locations via PendingIntent

The preview of this section is off trying to sweet-talk the Khaleesi into providing us with a dragon.

Are We There Yet? Are We There Yet? Are We There Yet?

The preview of this section is presently indisposed.

Testing… Testing…

The preview of this section was eaten by a grue.

Alternative Flavors of Updates

The preview of this section left for Hollywood to appear in a reality TV show.

The Fused Option

The preview of this section was lost due to a rupture in the space-time continuum.

Locations and Features

The preview of this section left for Hollywood to appear in a reality TV show.