久久久久久久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 - 可靠地獲取當前位置的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!

                  問題描述

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

                  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 的最后一個位置,它可能在任何地方(即,它可能距離用戶當前位置數(shù)英里).所以我會等待 requestLocationUpdates 回調(diào),如果它們在兩分鐘內(nèi)不存在,請移除偵聽器并放棄,對嗎?

                  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?

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

                  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.

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

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

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

                  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模板網(wǎng)!

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

                  相關文檔推薦

                  Help calculating X and Y from Latitude and Longitude in iPhone(幫助從 iPhone 中的緯度和經(jīng)度計算 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 或網(wǎng)絡提供商)

                    <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>
                          • 主站蜘蛛池模板: 亚洲成人免费视频在线观看 | 欧美色综合 | 亚洲国产一区二区视频 | 日本三级精品 | 日韩精品一区二区三区高清免费 | 日韩精品一区二区三区中文字幕 | 四虎影院在线播放 | 国产一区二区三区精品久久久 | 久久精品二区亚洲w码 | 久久激情网 | 国产一区二区在线免费观看 | 国产精品成人69xxx免费视频 | 欧美亚洲国产一区二区三区 | 免费久久久 | 狠狠色狠狠色综合日日92 | 91在线精品一区二区 | 久久福利 | 亚洲天堂中文字幕 | 欧美精品在线播放 | 亚洲欧美日韩一区二区 | 午夜精品久久久久久不卡欧美一级 | 天天射夜夜操 | 国产片一区二区三区 | 精品日韩一区 | 亚洲国产成人av好男人在线观看 | 欧美福利 | 国产综合在线视频 | 精品久久久久久中文字幕 | 亚洲xx在线 | 国产电影一区二区 | 国产精品视频在线播放 | 国产精品久久久一区二区三区 | 国产高清精品一区二区三区 | 黄色小视频入口 | 国产精品视频久久久久久 | 国产精品美女久久久久久不卡 | 日韩国产在线观看 | 国产一区二区三区亚洲 | 亚洲人成在线观看 | 奇米视频777| 中文字幕一区在线观看视频 |