久久久久久久av_日韩在线中文_看一级毛片视频_日本精品二区_成人深夜福利视频_武道仙尊动漫在线观看

    <i id='rKara'><tr id='rKara'><dt id='rKara'><q id='rKara'><span id='rKara'><b id='rKara'><form id='rKara'><ins id='rKara'></ins><ul id='rKara'></ul><sub id='rKara'></sub></form><legend id='rKara'></legend><bdo id='rKara'><pre id='rKara'><center id='rKara'></center></pre></bdo></b><th id='rKara'></th></span></q></dt></tr></i><div class="qwawimqqmiuu" id='rKara'><tfoot id='rKara'></tfoot><dl id='rKara'><fieldset id='rKara'></fieldset></dl></div>

      <bdo id='rKara'></bdo><ul id='rKara'></ul>
  • <legend id='rKara'><style id='rKara'><dir id='rKara'><q id='rKara'></q></dir></style></legend>

  • <small id='rKara'></small><noframes id='rKara'>

  • <tfoot id='rKara'></tfoot>

        Android - 可靠地獲取當前位置

        Android - Reliably getting the current location(Android - 可靠地獲取當前位置)
        <tfoot id='YIt4i'></tfoot>
          <bdo id='YIt4i'></bdo><ul id='YIt4i'></ul>
            <tbody id='YIt4i'></tbody>

            1. <legend id='YIt4i'><style id='YIt4i'><dir id='YIt4i'><q id='YIt4i'></q></dir></style></legend>

                <small id='YIt4i'></small><noframes id='YIt4i'>

              1. <i id='YIt4i'><tr id='YIt4i'><dt id='YIt4i'><q id='YIt4i'><span id='YIt4i'><b id='YIt4i'><form id='YIt4i'><ins id='YIt4i'></ins><ul id='YIt4i'></ul><sub id='YIt4i'></sub></form><legend id='YIt4i'></legend><bdo id='YIt4i'><pre id='YIt4i'><center id='YIt4i'></center></pre></bdo></b><th id='YIt4i'></th></span></q></dt></tr></i><div class="qwawimqqmiuu" id='YIt4i'><tfoot id='YIt4i'></tfoot><dl id='YIt4i'><fieldset id='YIt4i'></fieldset></dl></div>

                  本文介紹了Android - 可靠地獲取當前位置的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!

                  問題描述

                  我的應用會在特定時間檢查用戶是否在給定位置.我使用警報管理器來啟動進行此調用的服務:

                  My app checks at a specific time whether a user is at a given location. I use the alarm manager to start a service that makes this call:

                  locationManager.requestLocationUpdates(bestProvider, 0, 0, listener);
                  

                  并且還檢查:

                   locationManager.getLastKnownLocation(bestProvider);
                  

                  但我在真實設備上運行時遇到問題.一方面,getLastKnownLocation 很可能是 GPS 的最后一個位置,它可能在任何地方(即,它可能距離用戶當前位置數英里).所以我會等待 requestLocationUpdates 回調,如果它們在兩分鐘內不存在,請移除偵聽器并放棄,對嗎?

                  But I'm having problems when running on a real device. For one thing, getLastKnownLocation is most likely the last place the GPS was on, which could be anywhere (i.e., it could be miles from the user's current location). So I'll just wait for requestLocationUpdates callbacks, and if they aren't there within two minutes, remove the listener and give up, right?

                  錯了,因為如果用戶的位置已經穩定(即他們最近使用了 GPS 并且沒有移動),那么我的監聽器將永遠不會被調用,因為位置不會改變.但是 GPS 會一直運行,直到我的監聽器被移除,耗盡電池...

                  Wrong, because if the user's location is already stable (i.e., they've used GPS recently and haven't moved) then my listener will never be called because the location doesn't change. But the GPS will run until my listener is removed, draining the battery...

                  在不將舊位置誤認為當前位置的情況下,獲取當前位置的正確方法是什么?我不介意等幾分鐘.

                  What is the right way to get the current location without mistaking an old location for the current location? I don't mind waiting a few minutes.

                  我可能錯了沒有調用聽眾,這可能比我想象的要長一點......很難說.我仍然希望得到一個明確的答案.

                  It's possible that I'm wrong about the listener not being called, it may just take a little longer than I thought... Hard to say. I'd appreciate a definitive answer still.

                  推薦答案

                  代碼可能是這樣的:

                  public class MyLocation {
                      Timer timer1;
                      LocationManager lm;
                  
                      public boolean getLocation(Context context)
                      {
                          lm = (LocationManager) context.getSystemService(Context.LOCATION_SERVICE);
                          lm.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, locationListenerGps);
                          timer1=new Timer();
                          timer1.schedule(new GetLastLocation(), 20000);
                          return true;
                      }
                  
                      LocationListener locationListenerGps = new LocationListener() {
                          public void onLocationChanged(Location location) {
                              timer1.cancel();
                              lm.removeUpdates(this);
                              //use location as it is the latest value
                          }
                          public void onProviderDisabled(String provider) {}
                          public void onProviderEnabled(String provider) {}
                          public void onStatusChanged(String provider, int status, Bundle extras) {}
                      };
                  
                      class GetLastLocation extends TimerTask {
                          @Override
                          public void run() {
                               lm.removeUpdates(locationListenerGps);
                               Location location=lm.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
                               //use location as we have not received the new value from listener
                          }
                      }
                  }
                  

                  我們啟動監聽器并等待更新一段時間(在我的示例中為 20 秒).如果我們在此期間收到更新,我們會使用它.如果在此期間我們沒有收到更新,我們會使用 getLastKnownLocation 值并停止監聽器.

                  We start the listener and wait for update for some time (20 seconds in my example). If we receive update during this time we use it. If we don't receive an update during this time we use getLastKnownLocation value and stop the listener.

                  您可以在此處查看我的完整代碼 在 Android 上獲取用戶當前位置的最簡單、最可靠的方法是什么?

                  You can see my complete code here What is the simplest and most robust way to get the user's current location on Android?

                  編輯(由提問者):這是大部分答案,但我的最終解決方案使用 Handler 而不是 Timer.

                  EDIT (by asker): This is most of the answer, but my final solution uses a Handler instead of a Timer.

                  這篇關于Android - 可靠地獲取當前位置的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!

                  【網站聲明】本站部分內容來源于互聯網,旨在幫助大家更快的解決問題,如果有圖片或者內容侵犯了您的權益,請聯系我們刪除處理,感謝您的支持!

                  相關文檔推薦

                  Help calculating X and Y from Latitude and Longitude in iPhone(幫助從 iPhone 中的緯度和經度計算 X 和 Y)
                  Get user#39;s current location using GPS(使用 GPS 獲取用戶的當前位置)
                  IllegalArgumentException thrown by requestLocationUpdate()(requestLocationUpdate() 拋出的 IllegalArgumentException)
                  How reliable is LocationManager#39;s getLastKnownLocation and how often is it updated?(LocationManager 的 getLastKnownLocation 有多可靠,多久更新一次?)
                  CLLocation returning negative speed(CLLocation 返回負速度)
                  How to detect Location Provider ? GPS or Network Provider(如何檢測位置提供者?GPS 或網絡提供商)

                    <small id='GK0AK'></small><noframes id='GK0AK'>

                    <legend id='GK0AK'><style id='GK0AK'><dir id='GK0AK'><q id='GK0AK'></q></dir></style></legend>

                    <i id='GK0AK'><tr id='GK0AK'><dt id='GK0AK'><q id='GK0AK'><span id='GK0AK'><b id='GK0AK'><form id='GK0AK'><ins id='GK0AK'></ins><ul id='GK0AK'></ul><sub id='GK0AK'></sub></form><legend id='GK0AK'></legend><bdo id='GK0AK'><pre id='GK0AK'><center id='GK0AK'></center></pre></bdo></b><th id='GK0AK'></th></span></q></dt></tr></i><div class="qwawimqqmiuu" id='GK0AK'><tfoot id='GK0AK'></tfoot><dl id='GK0AK'><fieldset id='GK0AK'></fieldset></dl></div>
                    <tfoot id='GK0AK'></tfoot>

                          <tbody id='GK0AK'></tbody>

                          <bdo id='GK0AK'></bdo><ul id='GK0AK'></ul>
                          • 主站蜘蛛池模板: 91视频在线| 亚洲欧美日韩精品久久亚洲区 | 一区二区三区国产好的精 | 日本久久久一区二区三区 | 国户精品久久久久久久久久久不卡 | 欧美日韩亚洲一区 | 97伦理影院 | 亚洲精品一区在线 | 欧美日韩国产一区二区三区 | 国产免费一区二区三区网站免费 | 欧美一级三级在线观看 | 91网站在线播放 | 精品久久国产老人久久综合 | 免费观看黄网站 | 日本一区二区视频 | 亚洲视频网 | 国产日韩欧美一区 | 色免费在线视频 | 淫片专区| 欧美一区二区三区小说 | 干干干操操操 | 九九热在线免费观看 | 97精品视频在线 | www.狠狠操| 午夜网站视频 | 国产福利在线视频 | 韩国久久| 国产视频二区 | 精品国产乱码久久久 | 国产女人与拘做视频免费 | 日韩不卡在线 | 日韩欧美一区二区在线播放 | 亚洲人成网亚洲欧洲无码 | 久久久久久亚洲 | 国产中文视频 | 我要看黄色录像一级片 | 日本成人综合 | 国产精品久久av | 91天堂网| 在线观看国产www | 黄色一级大片视频 |