运行gps提供程序测试会导致拒绝访问模拟位置

时间:2016-11-16 02:23:53

标签: android unit-testing gps mocking

我试图通过将测试用例设置为模拟位置并根据准确性查看结果来调试在现场生成难以找到的位置的程序。

它依赖于后台服务来获取位置。

结果如下:

错误:

java.lang.SecurityException: com.vrin.location from uid 10168 not allowed to perform MOCK_LOCATION

AndroidManifest.xml(调试)

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

测试因上述错误而失败。是否因为我没有选择我不知道的模拟位置应用程序。产生错误的代码:

public void getMockLocation(Location location) {
    mLocationManager.removeTestProvider(LocationManager.GPS_PROVIDER);
    mLocationManager.addTestProvider
            (
                    LocationManager.GPS_PROVIDER,
                    "requiresNetwork" == "",
                    "requiresSatellite" == "",
                    "requiresCell" == "",
                    "hasMonetaryCost" == "",
                    "supportsAltitude" == "",
                    "supportsSpeed" == "",
                    "supportsBearing" == "",

                    android.location.Criteria.POWER_LOW,
                    android.location.Criteria.ACCURACY_FINE
            );

    Location newLocation = new Location(LocationManager.GPS_PROVIDER);

    newLocation.setLatitude (location.getLatitude());
    newLocation.setLongitude(location.getLongitude());

    newLocation.setAccuracy(500);

    mLocationManager.setTestProviderEnabled
            (
                    LocationManager.GPS_PROVIDER,
                    true
            );

    mLocationManager.setTestProviderStatus
            (
                    LocationManager.GPS_PROVIDER,
                    LocationProvider.AVAILABLE,
                    null,
                    System.currentTimeMillis()
            );

    mLocationManager.setTestProviderLocation
            (
                    LocationManager.GPS_PROVIDER,
                    newLocation
            );
}

提前感谢您的帮助!

1 个答案:

答案 0 :(得分:0)

可能的原因:

权限必须在<application>元素之外。

当然,您还需要通过“设置”进行设置。按照内联:

enable Mock Locations from Settings -> Applications -> Development

注意* 这可能因手机而异。

也没有使用它,但可能是你需要为android的上层版本设置运行时权限。