在 Android 中测试 GPS

发布于 2024-08-16 03:05:20 字数 258 浏览 4 评论 0原文

如何在 Android 中测试 GPS 应用程序?我们可以使用 Android 模拟器 对其进行测试吗?

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

蓝天白云 2024-08-23 03:05:20

是的。

如果您使用 Eclipse ADT 插件进行开发,请在启动模拟器后打开 DDMS 透视图并查找 Emulator Control 视图。有一个位置控制部分,允许您发送固定的纬度和经度坐标,如果您想定期发送多个坐标(以模拟特定路线),则可以发送 GPX(GPS 交换格式)或 KML(Keyhole 标记语言)文件。

或者,您可以简单地 telnet 到模拟器并从命令行使用 geo 命令,它支持固定的纬度和经度坐标以及 NMEA 数据语句:

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

Yes.

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
烟柳画桥 2024-08-23 03:05:20

也许您想使用单元测试,所以您可以尝试以下操作:

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");
        location.setLatitude(10.0);
        location.setLongitude(20.0);
        locationManager.setTestProviderLocation("Test", location);

        // Check if your listener reacted the right way

        locationManager.removeTestProvider("Test");
    }
}

为此,您还需要 AndroidManifest.xml 所需的权限:

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

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");
        location.setLatitude(10.0);
        location.setLongitude(20.0);
        locationManager.setTestProviderLocation("Test", location);

        // Check if your listener reacted the right way

        locationManager.removeTestProvider("Test");
    }
}

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

<uses-permission android:name="android.permission.ACCESS_MOCK_LOCATION" />
与往事干杯 2024-08-23 03:05:20

也许这个测试应用程序或这个可以帮助你。

Perhaps this test application or this can help you.

九命猫 2024-08-23 03:05:20

除了 Mallox 代码之外,这是除了真实 GPS 位置之外的模拟位置的解决方案:
一个字符串,

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) {
            return;
        }
        Location loc = lm.getLastKnownLocation((String) provider);
        if  (provider.equals(TEST_MOCK_GPS_LOCATION))  {
            mLastLocation = loc;
        }
    } 

TEST_MOCK_GPS_LOCATION 是测试类中的

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);
        location.setLatitude(34.1233400);
        location.setLongitude(15.6777880);
        location.setAccuracy(7);
        location.setTime(8);
        location.setElapsedRealtimeNanos(SystemClock.elapsedRealtimeNanos());

        locationManager.setTestProviderLocation(TEST_MOCK_GPS_LOCATION, location);

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

它是: }

希望对您有帮助

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) {
            return;
        }
        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);
        location.setLatitude(34.1233400);
        location.setLongitude(15.6777880);
        location.setAccuracy(7);
        location.setTime(8);
        location.setElapsedRealtimeNanos(SystemClock.elapsedRealtimeNanos());

        locationManager.setTestProviderLocation(TEST_MOCK_GPS_LOCATION, location);

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

}

hope it will help you

神回复 2024-08-23 03:05:20

在模拟器的扩展控件中(单击模拟器旁边的控制栏上的“...”)有“位置”选项卡,您可以在其中手动输入各个位置(纬度/经度/海拔)并将其发送到虚拟模拟器设备,或从可导入的 GPX 或 KML 文件中以所需速度设置 GPS 数据播放。

编辑:这是我所说的默认 Android 模拟器。

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.

初熏 2024-08-23 03:05:20

我建议您编写测试,将位置对象发送到您要测试的侦听器类。考虑到这一点,您将需要一些能够生成位置对象的辅助方法。

例如:

首先您发送一个具有固定坐标的位置(例如纽约中心)。

// 测试一切是否正常

然后发送一个距第一个坐标 100m 的坐标。您可以使用我最近实现的功能来做到这一点: https://stackoverflow.com/a/17545955/322791

// test如果一切正常

GPS 方法与时间相关是很常见的。我建议您在侦听器类上实现 getNow() 方法,并在需要当前时间时调用它。

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

在单元测试环境中,您只需将 obj.isUnitTesting=true 和 obj.unitTestTime 设置为您想要的任何时间。之后,您可以将第一个位置发送给侦听器,然后更改时间(+x 秒)并发送另一个位置...

以这种方式测试您的应用程序会更容易。

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.

E.g.:

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 {
         System.getcurrenttimemillis();
    }
}

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文