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_2CPU/ABI: Google APIs ARM (armeabi-v7a)Path: /Users/Ward/.android/avd/Nexus_6_API_22_2.avdTarget: Google APIs (API level 22)Skin: nexus_6SD Card: 100MSnapshot: nohw.lcd.density: 560hw.dPad: noavd.ini.encoding: UTF-8hw.camera.back: nonedisk.dataPartition.size: 200Mhw.gpu.enabled: yesruntime.network.latency: noneskin.dynamic: nohw.keyboard: yeshw.cpu.model: cortex-a8runtime.network.speed: fullhw.device.hash2: MD5:9b564b60b1aebee32c73ded9daa1e620hw.ramSize: 1536tag.id: google_apistag.display: Google APIshw.sdCard: yeshw.device.manufacturer: Googlehw.mainKeys: nohw.accelerometer: yeshw.trackBall: nohw.device.name: Nexus 6hw.sensors.proximity: yeshw.battery: yesAvdId: Nexus_6_API_22_2hw.sensors.orientation: yeshw.audioInput: yeshw.camera.front: nonehw.gps: yesavd.ini.displayname: Nexus 6 API 22 2snapshot.present: novm.heapSize: 256runtime.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.