在 Android 中测试 GPS
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1972895/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Testing GPS in Android
提问by Bharat Pawar
How do you test GPS applications in Android? Can we test it using the Android emulator?
您如何在Android 中测试 GPS 应用程序?我们可以使用Android 模拟器测试它吗?
回答by Jeff Gilfelt
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).
如果您使用 Eclipse ADT 插件进行开发,请在启动模拟器后打开 DDMS 透视图并查找 Emulator Control 视图。如果您想定期发送多个坐标(以模拟特定路线行驶),则有一个位置控制部分,允许您发送固定的纬度和经度坐标,或 GPX(GPS 交换格式)或 KML(锁孔标记语言)文件.
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 到模拟器并使用命令行中的 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
回答by Mallox
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:
为此,您还需要对 AndroidManifest.xml 具有必需的权限:
<uses-permission android:name="android.permission.ACCESS_MOCK_LOCATION" />
回答by spring26
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
除了 Mallox 代码之外,这是模拟位置以及真实 gps 位置的选项:TEST_MOCK_GPS_LOCATION 是一个字符串
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
希望它会帮助你
回答by Jasmin Vu?elj
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.
在模拟器的扩展控件中(单击模拟器旁边控制栏上的“...”)有位置选项卡,您可以在其中手动输入单个位置(纬度/经度/海拔)并将它们发送到虚拟设备,或从您可以导入的 GPX 或 KML 文件以所需速度设置 GPS 数据播放。
Edit: it's the default Android Emulator I'm talking about.
编辑:这是我正在谈论的默认 Android 模拟器。
回答by knagode
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
然后你发送一个距离第一个坐标 100m 的坐标。你可以用我最近实现的功能来做到这一点: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.
GPS 方法通常与时间有关。我建议您在侦听器类上实现方法 getNow() 并在需要当前时间时调用它。
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 ...
在单元测试环境中,您只需将 obj.isUnitTesting=true 和 obj.unitTestTime 设置为您想要的任何时间。之后,您可以将第一个位置发送给侦听器,然后更改时间(+x 秒)并发送另一个位置...
It will be much easier to test your application that way.
以这种方式测试您的应用程序会容易得多。