Play Services and Location Testing on Emulators

If you are using an AVD emulator, you must first make sure that your emulator images are up to date.

To do that, open up your SDK Manager (ToolsAndroidSDK Manager). Go down to the version of Android that you plan to use for your emulator and ensure that the Google APIs System Images are both installed and up to date (Figure 33.2). If an update is available, click the button to install the update and wait until it is ready to go before continuing.

Figure 33.2  Ensuring your emulator is up to date

Screenshot shows Android SDK Manager Window.

Your AVD emulator also needs to have a target OS version that supports the Google APIs. When you create an emulator, you can identify these target OS versions because they will say with Google APIs on the right. Choose one with an API level of 21 or higher and you will be all set (Figure 33.3).

Figure 33.3  Choosing a Google APIs image

Screenshot shows System Image page in Virtual Device Configuration window.

If you already have a suitable emulator but you had to update your images through the SDK earlier, you will need to restart your emulator for it to work.

Mock location data

On an emulator you will also need some dummy (or mock) location updates to work with. Android Studio provides an Emulator Control panel that lets you send location points to the emulator. This works great on the old location services, but it does nothing on the new Fused Location Provider. Instead, you have to publish mock locations programmatically.

We here at Big Nerd Ranch love to explain interesting things in minute detail. After the debacle that was Snipe Hunting: The Big Nerd Ranch Guide, though, we prefer to explain useful things. So instead of making you type out this mock location code, we have written it for you in a stand-alone app called MockWalker. To use it, download and install the APK at this URL:

The easiest way to do that is to open the browser app in your emulator and type in the URL (Figure 33.4).

Figure 33.4  Typing in the URL

Screenshot shows type in url in Android phone. The top of the screen shows the url typed in the address bar of a browser. Below is a list of urls. The bottom of the screen shows a touchscreen keyboard.

When it is done, press the download notification item in the toolbar to open the APK (Figure 33.5).

Figure 33.5  Opening the download

Screenshot shows opening the downloaded item. The screen shows Google Homepage in Android phone. A popup notification appears on top of the screen reading, MockWalker-debug.apk Download complete 1:31 PM.

MockWalker will trigger a mock walk for you via a service that posts mock location data to Fused Location Provider. It will pretend to walk in a loop around the Kirkwood neighborhood in Atlanta. While the service is running, any time Locatr asks Fused Location Provider for a location fix, it will receive a location posted by MockWalker.

Run MockWalker and press Start (Figure 33.6). Its service will keep running after you exit the app. (Do not exit the emulator, however. Leave the emulator running while you work on Locatr.) When you no longer need those mock locations, open MockWalker again and press the Stop button.

Figure 33.6  Running MockWalker

Screenshot shows Running Mockwalker. A text reads, MockWalker. A large Start button is placed at the center of the screen.

If you would like to know how MockWalker works, you can find its source code in the solutions folder for this chapter (see the section called Adding an Icon in Chapter 2 for more on the solutions). It uses a few interesting things: RxJava and a sticky foreground service to manage the ongoing location updates. If those sound interesting to you, check it out.

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

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