【问题标题】:SetTestProviderLocation Error...Location not being set despite all parameters?SetTestProviderLocation 错误...尽管所有参数都没有设置位置?
【发布时间】:2015-04-28 19:04:59
【问题描述】:

我正在尝试设置我手机的位置,但是它会抛出一个错误,说我缺少一些值?但是,这两个都是在我的方法中定义的,所以不确定我必须做什么......有什么想法吗?请解释一下?

类:

   //Initiates the method to set the phones location
    private void setMockLocation() {
      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 (55.9500);
        newLocation.setLongitude(3.1833);

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

错误:

04-28 19:52:13.716  17289-17289/com.example.ankhit.saveme E/AndroidRuntime﹕ FATAL EXCEPTION: main
    java.lang.IllegalArgumentException: Incomplete location object, missing timestamp or accuracy? Location[gps 55.9500,3.1833 acc=500 t=?!? et=?!?]
            at android.location.LocationManager.setTestProviderLocation(LocationManager.java:1218)
            at com.example.ankhit.saveme.UserLocation.setMockLocation(UserLocation.java:253)
            at com.example.ankhit.saveme.UserLocation.access$000(UserLocation.java:41)
            at com.example.ankhit.saveme.UserLocation$4.onClick(UserLocation.java:173)
            at android.view.View.performClick(View.java:4439)
            at android.view.View$PerformClick.run(View.java:18398)
            at android.os.Handler.handleCallback(Handler.java:725)
            at android.os.Handler.dispatchMessage(Handler.java:92)
            at android.os.Looper.loop(Looper.java:176)
            at android.app.ActivityThread.main(ActivityThread.java:5299)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:511)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1102)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:869)
            at dalvik.system.NativeStart.main(Native Method)

【问题讨论】:

  • 为什么 Android 位置需要准确?因为它不是 RMC NMEA 字符串中推荐的最小参数中的必要参数...?

标签: java android geolocation mocking locationmanager


【解决方案1】:

您是否在堆栈跟踪中看到以下内容:

java.lang.IllegalArgumentException: Incomplete location object, missing timestamp or accuracy? Location[gps 55.9500,3.1833 acc=500 t=?!? et=?!?]

从 API 17 开始,LocationManager 会检查 Location 对象(isComplete() 方法)。您的代码未能通过检查“t” - 时间和“et” - 以 nanos 为单位的实时时间。

设置时间很简单,只需这样做:mockLocation.setTime(System.currentTimeMillis());

设置经过时间仅在 API 17+ 中可用。如果您支持 API 较低(最低目标)的设备,则需要反射来添加此参数。

您需要做的是确保您的模拟 LocationManager 不会中断,只需检查设备是否为 API 17 或更高版本,然后选择三种方法之一(设置时间或忽略它):

if (Build.VERSION.SDK_INT > Build.VERSION_CODES.JELLY_BEAN) {
   mockLocation.setElapsedRealtimeNanos(SystemClock.elapsedRealtimeNanos());
   // Elapsed time can also be set using
   // mockLocation.setElapsedRealtimeNanos(System.nanoTime());
   // Elapsed time can be disregarded using
   // mockLocation.makeComplete();
}

最后一件事。您应该注释您的 setMockLocation() 方法以禁用这样的 Lint 检查:

@TargetApi(17)
public void setMockLocation() {
// etc

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-10-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-02-21
    • 1970-01-01
    • 1970-01-01
    • 2021-09-27
    相关资源
    最近更新 更多