【问题标题】:Running gps provider test produces denied access to mock location运行 gps 提供程序测试会拒绝访问模拟位置
【发布时间】:2016-11-16 02:23:52
【问题描述】:

我正在尝试调试一个程序,该程序通过设置测试用例来模拟位置并根据准确性查看结果,从而在现场产生不稳定的位置。

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

结果如下:

错误:

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
            );
}

提前感谢您的帮助!

内森

【问题讨论】:

  • 你在哪个版本上测试?
  • 嘿,我使用的是 23 版

标签: android unit-testing gps mocking


【解决方案1】:

可能的原因:

权限需要在&lt;application&gt;元素之外。

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

enable Mock Locations from Settings -> Applications -> Development

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

也没有用过,但可能需要你为更高版本的android设置运行时权限。

【讨论】:

    猜你喜欢
    • 2012-11-13
    • 1970-01-01
    • 1970-01-01
    • 2011-02-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-05-13
    相关资源
    最近更新 更多