33
Locations and Play Services

In this chapter, you will start writing a new app called Locatr that performs a Flickr geosearch. It will ask for runtime permission to find your current location, and then it will use that location to look for pictures nearby (Figure 33.1). Then, in the next chapter, you will show the picture on a map.

Figure 33.1  Locatr at the end of this chapter

Screenshot shows Locator app in Android phone. The Locator app screen shows the photo of a man wearing spectacles placed at the center.

It turns out that this simple job – finding your current location – is more interesting than you might expect. It requires integrating with Google’s set of libraries that live outside the standard library set, called Google Play Services.

Locations and Libraries

To see why, let’s talk a bit about what your average Android device can see and what tools Android gives you to see those things yourself.

Out of the box, Android provides a basic Location API. This API lets you listen to location data from a variety of sources. For most phones, those sources are fine location points from a GPS radio and coarse points from cell towers or WiFi connections. These APIs have been around for as long as Android itself. You can find them in the android.location package.

So the android.location APIs exist. But they fall short of perfection. Real-world applications make requests like, Use as much battery as you need to get as much accuracy as possible, or I need a location, but I would rather not waste my battery life. Rarely, if ever, do they need to make a request as specific as, Please fire up the GPS radio and tell me what it says.

This starts to be a problem when your devices move around. If you are outside, GPS is best. If you have no GPS signal, the cell tower fix may be best. And if you can find neither of those signals, it would be nicer to get by with the accelerometer and gyroscope than with no location fix at all.

In the past, high-quality apps had to manually subscribe to all of these different data sources and switch between them as appropriate. This was not straightforward or easy to do right.

Google Play Services

A better API was needed. However, if it were added to the standard library, it would take a couple of years for all developers to be able to use it. This was annoying, because the OS had everything that a better API would need: GPS, coarse location, and so forth.

Fortunately, the standard library is not the only way Google can get code into your hands. In addition to the standard library, it provides Google Play Services. This is a set of common services that are installed alongside the Google Play Store application. To fix the locations mess, Google shipped a new locations service in Play Services called the Fused Location Provider.

Because these libraries live in another application, you must actually have that application installed. This means that only devices with the Google Play Store app installed and up to date will be able to use your application. This almost certainly means that your app will be distributed through the Play Store, too. If your app is not available through the Play Store, you are unfortunately out of luck and will need to use another location API.

For this exercise, if you will be testing on a hardware device, make sure that you have an up-to-date Google Play Store app. But what if you are running on an emulator? Never fear – we will cover that in a moment.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.135.227.135