SetTestProviderLocation 错误...尽管所有参数都未设置位置?

SetTestProviderLocation Error...Location not being set despite all parameters?

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

Class:

   //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[=11=]0(UserLocation.java:41)
            at com.example.ankhit.saveme.UserLocation.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)

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

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" - 以纳米为单位的实时时间。

设置时间很简单,只需按照以下步骤操作即可: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