久久久久久久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>
                          • 主站蜘蛛池模板: 九九久久99 | 日日操夜夜操天天操 | 国产福利91精品一区二区三区 | 欧美视频网 | 色综合久久天天综合网 | 操操日| 国产精品日日摸夜夜添夜夜av | 亚洲一区精品视频 | 黄色三级毛片 | 综合久久网| 亚洲bt 欧美bt 日本bt | 国产精品一区二区av | 欧美成视频在线观看 | 精品久久久久一区二区国产 | 中文字幕一区二区三区在线观看 | 亚洲精品在线免费播放 | 成年视频在线观看 | 在线播放国产一区二区三区 | 香蕉视频一区二区 | 国产a一区二区 | 亚洲啪啪| 91视频a| 夜夜爽99久久国产综合精品女不卡 | 免费在线成人网 | 国产一区二区精品在线观看 | 黄色片免费在线观看 | 日韩视频精品 | 国产综合第一页 | 99久久久久| 国产视频久久 | 91夜夜夜 | 欧美激情亚洲天堂 | 国产精品亚洲精品久久 | 久久国产成人午夜av影院武则天 | 欧美另类视频在线 | 亚洲国产精品视频一区 | 国产欧美精品在线 | 亚洲精品一区在线 | 国产精品国产精品国产专区不蜜 | 日韩亚洲欧美综合 | 在线成人av |