Testing GPS in Android

How do you test GPS applications in Android? Can we test it using the Android emulator?

Asked by: Dexter607 | Posted: 24-01-2022

Answer 1


If you develop using the Eclipse ADT plugin, open the DDMS perspective after launching the emulator and look for the Emulator Control view. There is a location controls section that allows you to send fixed latitude and longitude coordinates, or GPX (GPS Exchange Format) or KML (Keyhole Markup Language) files if you want to send multiple coordinates at regular intervals (to simulate traveling a specific route).

Alternatively, you can simply telnet to the emulator and use the geo command from the command line, which supports fixed latitude and longitude coordinates as well as NMEA data sentences:

telnet localhost 5554
geo fix -82.411629 28.054553
geo nmea $GPGGA,001431.092,0118.2653,N,10351.1359,E,0,00,,-19.6,M,4.1,M,,0000*5B

Answered by: Anna344 | Posted: 25-02-2022

Answer 2

Maybe you want to a use a Unit test, so you can try this:

public class GPSPollingServiceTest extends AndroidTestCase {
    private LocationManager locationManager;

    public void testGPS() {
        LocationManager locationManager = (LocationManager) this.getContext().getSystemService(Context.LOCATION_SERVICE);
        locationManager.addTestProvider("Test", false, false, false, false, false, false, false, Criteria.POWER_LOW, Criteria.ACCURACY_FINE);
        locationManager.setTestProviderEnabled("Test", true);

        // Set up your test

        Location location = new Location("Test");
        locationManager.setTestProviderLocation("Test", location);

        // Check if your listener reacted the right way


For this to work you also need a required permission to the AndroidManifest.xml:

<uses-permission android:name="android.permission.ACCESS_MOCK_LOCATION" />

Answered by: Daisy953 | Posted: 25-02-2022

Answer 3

Perhaps this test application or this can help you.

Answered by: Rubie323 | Posted: 25-02-2022

Answer 4

in addition to Mallox code this is the sulotion for mock location in addition to real gps location : TEST_MOCK_GPS_LOCATION is a string

LocationManager lm = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
    Location location = null;

    List providers = lm.getAllProviders();
    for (Object provider : providers) {
        if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
        Location loc = lm.getLastKnownLocation((String) provider);
        if  (provider.equals(TEST_MOCK_GPS_LOCATION))  {
            mLastLocation = loc;

in the test class it is :

public class GPSPollingServiceTest extends AndroidTestCase {
private LocationManager locationManager;

public void testGPS() throws NoSuchMethodException, InvocationTargetException, IllegalAccessException {
    LocationManager locationManager = (LocationManager) this.getContext().getSystemService(Context.LOCATION_SERVICE);
    List providers = locationManager.getAllProviders();
    if(!providers.contains(TEST_MOCK_GPS_LOCATION)) {
        locationManager.addTestProvider(TEST_MOCK_GPS_LOCATION, false, false, false, false, false, false, false, Criteria.POWER_LOW, Criteria.ACCURACY_FINE);
        locationManager.setTestProviderEnabled(TEST_MOCK_GPS_LOCATION, true);
        // Set up your test
        Location location = new Location(TEST_MOCK_GPS_LOCATION);

        locationManager.setTestProviderLocation(TEST_MOCK_GPS_LOCATION, location);

        Method locationJellyBeanFixMethod = Location.class.getMethod("makeComplete");
        if (locationJellyBeanFixMethod != null) {
    } else {
        // Check if your listener reacted the right way


hope it will help you

Answered by: Kristian847 | Posted: 25-02-2022

Answer 5

In the Extended controls of your emulator (click "..." on the control bar next to your emulator) there is the Location tab, where you can either manually input individual locations (latitude / longitude / altitude) and send them to the virtual device, or setup GPS data playback at a desired speed from a GPX or a KML file you can import.

Edit: it's the default Android Emulator I'm talking about.

Answered by: Ted209 | Posted: 25-02-2022

Answer 6

I suggest that you write test which is sending location object to the listener class you want to test. With this in mind you will need some helper methods that will be able to produce location objects.


First you send a location with fixed coordinates (e.g. center of New York).

// test if everything is OK

Than you send a coordinate which is 100m appart from the first coordinate. You can do this with function I implemented recently: https://stackoverflow.com/a/17545955/322791

// test if everything is OK

It is common that GPS methods are related to time. I suggest that you implement method getNow() on listener class and call it whenever you need current time.

long getNow() {
    if (isUnitTesting) {
         return unitTestTime;
    } else {

In unit test environment you simply set the obj.isUnitTesting=true and obj.unitTestTime to any time you want. After that you can send first location to the listener, then you change time (+x seconds) and send another location ...

It will be much easier to test your application that way.

Answered by: Kellan634 | Posted: 25-02-2022

Similar questions

testing - What is correct xmlns url for AdMob in Android Layout XML

I am testing the AdMob for Android SDK. I can't set the admob:testing="true" because the admob attribute is unknown. &lt;LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:admob="http://schemas.android.com/apk/res/org.ifies.android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" android:background="#FF...

unit testing - Why do I get a Illegal Access Error when running my Android tests?

I get the following stack trace when running my Android tests on the Emulator: java.lang.NoClassDefFoundError: client.HttpHelper at client.Helper.&lt;init&gt;(Helper.java:14) at test.Tests.setUp(Tests.java:15) at android.test.AndroidTestRunner.runTest(AndroidTestRunner.java:164) at android.test.AndroidTestRunner.runTest(AndroidTestRunner.java:151) at android.test.InstrumentationTestRunner.onStart(Instrume...

unit testing - Android: test suite

I have realize different tests extending the "ActivityInstrumentationTestCase2". Now, I want to structure my tests: group them and be able to run a subset. How to do this? Can I use the TestSuite class? Any idea? Regards, Alban.

Testing FPS using Android plugin for Eclipse?

Ive no doubt this question may have been addressed before but how can I turn on a framerate monitor to use when I run my programs using the android emulator so I can see exactly what my android game is achieving at a given time?

Android app unit testing

So, I'm new to android unit testing. I'm trying to write a unit test for the Phone application: package com.android.phone; import android.content.Intent; import android.net.Uri; import android.test.ApplicationTestCase; import android.test.suitebuilder.annotation.MediumTest; import com.android.phone.PhoneApp; import dalvik.annotation.TestTargetClass; @TestTargetClass(PhoneApp.class) public class TestPhon...

Hit testing on a MapView, Android

I hava a MapView and I define a Rect. Touching the map I compare the coordinates to detect whether the rect is touched or not. But it does not work RectF hitTestRecr = new RectF(); hitTestRecr.set(0,100,0,100); hitTestRecr.offset(0,0); if (hitTestRecr.contains(event.getX(),event.getY())) { Toast.makeText(getBaseContext(), "hit", Toast.LENGTH_SHORT).show(); }else{ Toast.makeText(getBaseContext(), "...

unit testing - Access image in my Android Tests

I am working on an Android app. It has corresponding spec/test application. As part of some of my tests, I need to pick up an image from my assets folder and calculate SHA-1 for it. I can calculate SHA, as long as I can pick the image. Since the tests run on emulator; I am not sure how to pick the image in my test. Does anyone have any idea, how I can go about it. With and without AssetManager maybe? Any ideas wil...

Live Testing of the Android LVL

I have a paid app that's been out in the Android Market for a few months now, and up to this point that app had no type of copy protection or license verification scheme. I have just finished adding an modified version of the LVL code to my app. To test out the code's license response handling, I signed into my dev account on the emulator and went through all of the test responses successfully, and then did the sam...

unit testing - JUnit in android

I am familiar with JUnit testing in android.. My Question is if we are using calculator and we want to test addition operation..To test the addition operation if we are using more number of test cases(for example 30). instead of rewriting the test cases for 30 times, is there any generic way to do this or is there any way to take the test cases form excel sheet or xml file..? Please let me know is there any better ...

Testing my Java/Android app - is my setup wrong?

I just tried to test my first android app using the emulator, it crashed (as expected). So I load the debug view in Eclipse and now I'm not really sure what I'm looking for... I see a tab that within the first thread I see a bunch of executions (i guess that's the right word) and they say: Source not found. and has a button that reads Edit Source Lookup Path... Is there something ...

Still can't find your answer? Check out these communities...

Android Google Support | Android Community | Android Community (Facebook) | Dev.io Android