简体   繁体   English

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

[英]SetTestProviderLocation Error…Location not being set despite all parameters?

I am trying to set my phone's location however, It throws an error saying I am missing some values? 我试图设置我的手机的位置但它会抛出一个错误,说我错过了一些值? However, both of those are defined in my method so not to sure what I must do...Any ideas? 但是,这两个都是在我的方法中定义的,所以不确定我必须做什么......有什么想法吗? With an explanation please? 有解释吗?

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

Error: 错误:

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)

Do you see the following in your stacktrace: 您是否在堆栈跟踪中看到以下内容:

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

Starting with API 17, the LocationManager does a check on the Location object ( isComplete() method). 从API 17开始,LocationManager检查Location对象( isComplete()方法)。 Your code is failing the checks for "t" - time and "et" - elapsed real time in nanos. 您的代码未通过“t” - 时间和“et”的检查 - 以纳米为单位实时流逝。

Setting the time is easy, just do something like this: mockLocation.setTime(System.currentTimeMillis()); 设置时间很简单,只需执行以下操作: mockLocation.setTime(System.currentTimeMillis());

Setting the elapsed time is only available in API 17+. 设置已用时间仅适用于API 17+。 If you support devices with a lower API (minimum target), you will need reflection to add this parameter. 如果您支持API较低(最小目标)的设备,则需要反射才能添加此参数。

What you need to do to make sure your mock LocationManager doesn't break, is simply check if the device is API 17 or greater and then choose one of three methods (either to set the time or disregard it): 您需要做的是确保您的模拟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();
}

One last thing. 最后一件事。 You should annotate your setMockLocation() method to disable Lint checks like this: 您应该注释setMockLocation()方法以禁用这样的Lint检查:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 尽管已设置依赖项,Wildfly 10中的ClassNotFoundException - ClassNotFoundException in Wildfly 10 despite dependency being set 尽管设置了-Xmx,但Heroku错误R14(超出内存配额) - Heroku Error R14 (Memory quota exceeded) despite -Xmx being set 尽管已初始化,Firebase 仍抛出未初始化的错误 - Firebase throwing uninitialized error despite being initialized 尽管回收了所有视图,还是调用了Recyclerview onCreateViewHolder? - Recyclerview onCreateViewHolder being called despite all views being recycled? 未在本机查询中设置参数 - Parameters not being set in native query 球衣2 getLinks()返回空集,尽管链接位于资源中 - Jersey 2 getLinks() returns empty set despite links being in the resource 尽管存在“级联所有”,但JPA中的外键约束问题 - Foreign key constraint issue in JPA despite “cascade all” being present Android - 尽管设置为匹配父级,但布局宽度环绕内容? - Android - Layout Width wrapping content despite being set to match parent? 尽管列表中的所有对象都相等,但 List.equals 为 false - List.equals is false despite all objects in list being equal 尽管dll位于正确的目录中,但链接错误仍不令人满意 - Unsatisfied Link Error despite dll being in the correct directory
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM