GpsStatus.Listener未在网络状态更改时触发

时间:2013-01-07 14:55:46

标签: android gps

我希望我的RTT应用程序能够控制何时/是否接收位置更新,无论是GPS还是网络。我可以使用GpsStatus.Listener轻松控制GPS状态,但我无法找到网络更新的等效物。例如,如果我们进入没有网络或wifi的区域,或者发生超时,就像GpsStatus.GPS_EVENT_SATELLITE_STATUS所涵盖的情况一样,事件将会触发

代码片段:

    mGPSReceiver = (LocationManager)this.getSystemService(Context.LOCATION_SERVICE);
    mLocationListener = new LocationListener() {
        private Location mLastLocation;

        @Override
        public void onLocationChanged(final Location location) {
            // STUFF
        }

        @Override
        public void onProviderDisabled(final String provider) {
            if (mGPSReceiver.getProviders(true).size() == 0) { // WRONG
                PhoneStatusService.this.setGPSStatus(false);
            }
        }

        @Override
        public void onProviderEnabled(final String provider) {
        }

        @Override
        public void onStatusChanged(final String provider, final int status, final Bundle extras) {
        }
    };
    mGPSListener = new GpsStatus.Listener() {
        @Override
        public void onGpsStatusChanged(final int event) {
            switch (event) {
                case GpsStatus.GPS_EVENT_STARTED:
                    break;
                case GpsStatus.GPS_EVENT_FIRST_FIX:
                    setGPSStatus(true);
                    break;
                case GpsStatus.GPS_EVENT_STOPPED:
                    break;
                case GpsStatus.GPS_EVENT_SATELLITE_STATUS:
                    final boolean isGPSFixing = (SystemClock.elapsedRealtime() - mLastLocationMillis) < GPS_TIMEOUT;
                    setGPSStatus(isGPSFixing);
                    break;
                default:
                    break;
            }
        }
    };
    mGPSReceiver.addGpsStatusListener(mGPSListener);
    mGPSReceiver.requestLocationUpdates(LocationManager.GPS_PROVIDER, GPS_FIX_TIME, 0,
            mLocationListener);
    mGPSReceiver.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, GPS_FIX_TIME, 0,
            mLocationListener);

1 个答案:

答案 0 :(得分:-2)

创建接收网络更新的BroadCastReceiver。