pbootcms网站模板|日韩1区2区|织梦模板||网站源码|日韩1区2区|jquery建站特效-html5模板网

    <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="rvdtjjp" 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 - 可靠地獲取當(dāng)前位置

        Android - Reliably getting the current location(Android - 可靠地獲取當(dāng)前位置)
        <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="lf7jndl" id='YIt4i'><tfoot id='YIt4i'></tfoot><dl id='YIt4i'><fieldset id='YIt4i'></fieldset></dl></div>

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

                  問題描述

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

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

                  但我在真實設(shè)備上運行時遇到問題.一方面,getLastKnownLocation 很可能是 GPS 的最后一個位置,它可能在任何地方(即,它可能距離用戶當(dāng)前位置數(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)聽器將永遠(yuǎ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...

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

                  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 上獲取用戶當(dāng)前位置的最簡單、最可靠的方法是什么?

                  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.

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

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

                  相關(guā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 獲取用戶的當(dāng)前位置)
                  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 返回負(fù)速度)
                  How to detect Location Provider ? GPS or Network Provider(如何檢測位置提供者?GPS 或網(wǎng)絡(luò)提供商)

                    <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="jpfbrnd" 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>
                          • 主站蜘蛛池模板: 注浆压力变送器-高温熔体传感器-矿用压力传感器|ZHYQ朝辉 | 色油机-色母机-失重|称重式混料机-称重机-米重机-拌料机-[东莞同锐机械]精密计量科技制造商 | 建筑工程资质合作-工程资质加盟分公司-建筑资质加盟 | 纸箱网 -纸箱机械|设备|包装纸盒|包装印刷行业门户网站 | 压力喷雾干燥机,喷雾干燥设备,柱塞隔膜泵-无锡市闻华干燥设备有限公司 | 广州二手电缆线回收,旧电缆回收,广州铜线回收-广东益福电缆线回收公司 | 济南菜鸟驿站广告|青岛快递车车体|社区媒体-抖音|墙体广告-山东揽胜广告传媒有限公司 | 胶原检测试剂盒,弹性蛋白检测试剂盒,类克ELISA试剂盒,阿达木单抗ELISA试剂盒-北京群晓科苑生物技术有限公司 | 电动垃圾车,垃圾清运车-江苏速利达机车有限公司 | 气象监测系统_气象传感器_微型气象仪_气象环境监测仪-山东风途物联网 | 特种阀门-调节阀门-高温熔盐阀-镍合金截止阀-钛阀门-高温阀门-高性能蝶阀-蒙乃尔合金阀门-福建捷斯特阀门制造有限公司 | 品牌广告服务平台,好排名,好流量,好生意。 | 上海三信|ph计|酸度计|电导率仪-艾科仪器 | 有福网(yofus.com)洗照片冲印,毕业聚会纪念册相册制作个性DIY平台 | 电磁铁_小型推拉电磁铁_电磁阀厂家-深圳市宗泰电机有限公司 | 打孔器,打孔钳厂家【温州新星德牌五金工具】 | 置顶式搅拌器-优莱博化学防爆冰箱-磁驱搅拌器-天津市布鲁克科技有限公司 | 紫外线老化试验箱_uv紫外线老化试验箱价格|型号|厂家-正航仪器设备 | 双舌接地线-PC68数字式高阻计-ZC36|苏海百科 | 天品互联-北京APP开发公司-小程序开发制作-软件开发 | 英国公司注册-新加坡公司注册-香港公司开户-离岸公司账户-杭州商标注册-杭州优创企业 | 北京模型公司-工业模型-地产模型-施工模型-北京渝峰时代沙盘模型制作公司 | 粉丝机械,粉丝烘干机,粉丝生产线-招远市远东粉丝机械有限公司 | 环压强度试验机-拉链拉力试验机-上海倾技仪器仪表科技有限公司 | 无锡市珂妮日用化妆品有限公司|珂妮日化官网|洗手液厂家 | 钛合金标准件-钛合金螺丝-钛管件-钛合金棒-钛合金板-钛合金锻件-宝鸡远航钛业有限公司 | 衬塑管道_衬四氟管道厂家-淄博恒固化工设备有限公司 | 金蝶帐无忧|云代账软件|智能财税软件|会计代账公司专用软件 | 专业生物有机肥造粒机,粉状有机肥生产线,槽式翻堆机厂家-郑州华之强重工科技有限公司 | 基本型顶空进样器-全自动热脱附解吸仪价格-AutoHS全模式-成都科林分析技术有限公司 | 对夹式止回阀_对夹式蝶形止回阀_对夹式软密封止回阀_超薄型止回阀_不锈钢底阀-温州上炬阀门科技有限公司 | 厌氧工作站-通用型厌氧工作站-上海胜秋科学仪器有限公司 | 东莞画册设计_logo/vi设计_品牌包装设计 - 华略品牌设计公司 | 便携式表面粗糙度仪-彩屏硬度计-分体式粗糙度仪-北京凯达科仪科技有限公司 | 德国EA可编程直流电源_电子负载,中国台湾固纬直流电源_交流电源-苏州展文电子科技有限公司 | 除甲醛公司-甲醛检测治理-杭州创绿家环保科技有限公司-室内空气净化十大品牌 | 上海地磅秤|电子地上衡|防爆地磅_上海地磅秤厂家–越衡称重 | 杭州火蝠电商_京东代运营_拼多多全托管代运营【天猫代运营】 | 阴离子_阳离子聚丙烯酰胺厂家_聚合氯化铝价格_水处理絮凝剂_巩义市江源净水材料有限公司 | 网架支座@球铰支座@钢结构支座@成品支座厂家@万向滑动支座_桥兴工程橡胶有限公司 | 高楼航空障碍灯厂家哪家好_航空障碍灯厂家_广州北斗星障碍灯有限公司 |