關於系統提供的定位api不能實時返回定位狀態的情況

1、首先,系統提供了簡單實用的定位方法,使用起來很簡單

String serviceString = Context.LOCATION_SERVICE;
 locationManager = (LocationManager) getSystemService(serviceString);
 String provider = LocationManager.GPS_PROVIDER;
 locationManager.requestLocationUpdates(provider, 1000, 0, locationListener);
private final LocationListener locationListener = new LocationListener() {
        @Override
        public void onLocationChanged(Location location) {
            if (location.getLatitude() > 0.0f
                    && location.getLongitude() > 0.0f) {   // 定位成功
                isGpsSuccess = true;
                
                locationTime = location.getTime();
                longitude = location.getLongitude();
                latitude = location.getLatitude();
                altitude = location.getAltitude();
                accuracy = location.getAccuracy();
                
            }
        }

        @Override
        public void onStatusChanged(String provider, int status, Bundle extras) {
            LogUtil.info(TAG, "onStatusChanged.provider" + provider + " status: " + status);
        }

        @Override
        public void onProviderEnabled(String provider) {
            LogUtil.info(TAG, "onProviderEnabled.provider" + provider);
        }

        @Override
        public void onProviderDisabled(String provider) {
            LogUtil.info(TAG, "onProviderDisabled.provider" + provider);
        }
    };

很簡單,首先獲取定位服務,之後設置定位Provider,再就是請求定位監聽,

但是實用之後發現,這個方法不能實時獲取定位狀態的變更,在定位失敗時,onLocationChanged回調方法不走,這事需要例外一個監聽來輔助實現定位狀態的監聽

locationManager.addGpsStatusListener(gpsStatusListener);
private GpsStatus.Listener gpsStatusListener = new GpsStatus.Listener() {
        @Override
        public void onGpsStatusChanged(int event) {
            switch (event) {
                //衛星狀態改變
                case GpsStatus.GPS_EVENT_SATELLITE_STATUS:
                        //獲取當前狀態
                        GpsStatus gpsStatus = locationManager.getGpsStatus(null);
                        //獲取衛星顆數的默認最大值
                        int maxSatellites = 24;
                        //獲取所有的衛星
                        Iterator<GpsSatellite> iters = gpsStatus.getSatellites().iterator();
                        //衛星顆數統計
                        allCount = 0;
                        gpsCounts = 0;
                        bdCounts = 0;
                        gpsContent.clear();
                        bdContent.clear();
                        while (iters.hasNext() && allCount < maxSatellites) {
                            GpsSatellite s = iters.next();
                            if (s.getSnr() != 0) {
                                if (s.getPrn() >= 1 && s.getPrn() <= 37) {
                                    if (s.usedInFix()) {
                                        // GPS衛星的信噪比
                                        gpsContent.add((int) s.getSnr());
                                        gpsCounts++;
                                        allCount++;
                                    }
                                } else if (s.getPrn() >= 201 && s.getPrn() <= 237) {
                                    if (s.usedInFix()) {
                                        // BD衛星的信噪比
                                        bdContent.add((int) s.getSnr());
                                        bdCounts++;
                                        allCount++;
                                    }
                                }
                            }
                        }
                        if (allCount >= 3) {
                            isGpsSuccess = true;
                        } else {
                            isGpsSuccess = false;
                        }
                        LogUtil.info(TAG, "gpsStatusListener gpsCounts  " + gpsCounts + "bdCounts  " + bdCounts + "count  " + allCount);
                   
                    break;
                default:
                    break;
            }
        }
    };

添加衛星狀態的監聽,監測可用衛星數來輔助判斷監聽,可用衛星數大於等於3時可獲取到有效定位消息,其餘則判定爲定位失敗。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章