[android]SetTestProviderLocation 错误...不被设置尽管所有参数的位置吗?

标签: Java Android
发布时间: 2017/3/19 14:43:36
注意事项: 本文中文内容可能为机器翻译,如要查看英文原文请点击上面连接.

我想设置我的手机 location 然而,它抛出错误,并说我想念一些值吗?然而,这两个问题是我所以不知道的方法中定义自己必须做什么...有什么想法?请加解释吗?

类︰

   //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)

解决方法 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"-检查 time 和"et"-真正经过 time 在 nano。

设置 time 是容易,只是做这样的事情︰mockLocation.setTime(System.currentTimeMillis());

设置已用 time 是唯一可在 API 17+。如果你支持一个较低的 API (最低目标) 的设备,您将需要反射添加此参数。

你要确保你的模拟 LocationManager 不打破,是简单地检查,如果该设备是 17 或更高版本的 API,然后选择三种方法之一 (或者设置 time 或无视它):

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() 方法来禁用皮棉检查像这样︰

@TargetApi(17)
public void setMockLocation() {
// etc
赞助商