Testing Location Services with Android Virtual Devices

One of most powerful features of mobile devices in general and Android devices in particular is their ability to provide location-based functionality. However, before you can take advantage of these features in your apps you need to be able generate these events in your Android development environment. This article discusses how to enable location-based services in an emulated Android Phone and how to send location events from Android Studio.

But just hold your horses there, cowboy. Before we start slinging code, it is very important to get all of your code, build tools, the Google API, and the images for your virtual devices up to date. This is a real gotcha with Android. Carefully inspect your saddle and your Android SDK manager to ensure you have everything up to date for the version of the API that you’re going to be working with:

Particularly important for this sample are the System Images targeted at the Google APIs. Those system images are loaded with the Google Location APIs in place that we need for testing location-based applications. There’s not much you can do if you don’t have the right APIs loaded into your images — they’re virtual paperweights so to speak.

To practice our location testing, we’re going to work the Location Updates sample that can be found on GitHub:

https://github.com/googlesamples/android-play-location/tree/master/LocationUpdates

Once you’ve imported the app into your development environment, you’ll want to build the project. Use the Android Device Manager (AVD) to create a virtual environment to display your location events.

I’ve found that recent devices seem to work better than legacy ones. Some of my virtual devices that I’d previously created no longer seem to work after recent updates to Android Studio. I’ve recently created a Nexus 6 AVD with the Google API’s which seems to do the trick. To be honest, I had a beast of a time getting the AVD to launch, but eventually it worked and I was able to get the AVD logging enabled. Android Studio is like the Madonna of virtual memory — it just can’t get enough. Here are the details:

AVD Details:

Name: Nexus_6_API_22_2
CPU/ABI: Google APIs ARM (armeabi-v7a)
Path: /Users/Ward/.android/avd/Nexus_6_API_22_2.avd
Target: Google APIs (API level 22)
Skin: nexus_6
SD Card: 100M
Snapshot: no
hw.lcd.density: 560
hw.dPad: no
avd.ini.encoding: UTF-8
hw.camera.back: none
disk.dataPartition.size: 200M
hw.gpu.enabled: yes
runtime.network.latency: none
skin.dynamic: no
hw.keyboard: yes
hw.cpu.model: cortex-a8
runtime.network.speed: full
hw.device.hash2: MD5:9b564b60b1aebee32c73ded9daa1e620
hw.ramSize: 1536
tag.id: google_apis
tag.display: Google APIs
hw.sdCard: yes
hw.device.manufacturer: Google
hw.mainKeys: no
hw.accelerometer: yes
hw.trackBall: no
hw.device.name: Nexus 6
hw.sensors.proximity: yes
hw.battery: yes
AvdId: Nexus_6_API_22_2
hw.sensors.orientation: yes
hw.audioInput: yes
hw.camera.front: none
hw.gps: yes
avd.ini.displayname: Nexus 6 API 22 2
snapshot.present: no
vm.heapSize: 256
runtime.scalefactor: auto

To validate that location-related services are available on your virtual device, you should start up Google Maps. I always feel better when I see the little red hotspot symbol in the toolbar. Clicking the target will focus the map on the location where you phone thinks it is.

Now that your device is up and running — click Run->Run App (or the little green arrow) in Android Studio to port your code to the virtual device. Deploying your app should automatically start the application in your emulated device:

Click Start Updates in your app to allow receiving events in your application. The next thing to do is to start the Android Device Monitor. Once this is running, you should navigate to the Emulator Control tab. If you scroll down this tab a bit, you’ll see the Location Controls as shown below:

Using the Location Controls, you can send coordinates to your virtual Android Device. Here are the latest coordinates from the control:

Wonders never cease!! Enjoy.