【问题标题】:What does the following code do if there is no GPS fix..?如果没有 GPS 定位,以下代码会做什么?
【发布时间】:2010-10-22 19:12:05
【问题描述】:

如果没有 GPS FIX(因为该人在金属建筑物内或其他地方)......它是否只是留在 Looper 中......?或者它是否继续通过 requestLocationUpdates 尝试修复..??

如果我确实有一个好的 GPS FIX....我的代码可以正常工作...在 onLocationChanged()...我将当前位置更新到数据库。

另外...什么时候调用 onLocationChanged()..??是否仅在有 GPS FIX 时才调用..? 只是想知道..

    public void run()
    {
        Looper.prepare();

        LocationManager lm = (LocationManager)            getSystemService(Context.LOCATION_SERVICE);
        LocationListener ll = new mylocationlistener();
        lm.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, ll);

        Looper.loop();
    }

【问题讨论】:

    标签: android


    【解决方案1】:

    嗯,这是在生产代码中吗?或者你只是好奇。这绝对是不可接受的 android 代码。看看这个,了解如何正确实现 locationlistener

    http://hejp.co.uk/android/android-gps-example/

    when is onLocationChanged() called

    当您传递给requestLocationUpdates(String provider, long minTime, float minDistance, PendingIntent intent) 方法的参数得到满足时,将调用Onlocationchanged。也就是说,只要 minTime 过去或设备移动 minDistance。 (注意这些只是提示,不是确切的值)

    【讨论】:

    • 这不是生产代码。但是我的代码有什么问题..??我将其作为服务运行。它抱怨说我没有安装 Looper……所以这就是我所做的。我的代码似乎工作正常。我只是想知道当没有 GPS 修复时我的代码发生了什么。
    • 首先,这段代码看起来像是在某种线程中。我猜这是因为它在 run 方法中。 Requestlocationupdates 是异步的,所以它不需要在线程中。这可能就是你关于 looper 的错误。你的代码可能会工作,但你可以让很多写得很糟糕的代码工作。让你的代码编译和运行不应该是衡量成功的标准。
    【解决方案2】:

    我发现(经过大量测试)GPS 失败了……它会通过一个错误使我的服务崩溃。而不是杀死应用程序...我只是重新启动服务并再试一次。

    【讨论】:

      猜你喜欢
      • 2020-02-21
      • 1970-01-01
      • 1970-01-01
      • 2014-08-31
      • 2021-03-20
      • 2013-10-18
      • 1970-01-01
      • 1970-01-01
      • 2011-12-27
      相关资源
      最近更新 更多