GpsStatus.Listener not firing on network status changes

跟風遠走 提交于 2019-12-02 06:37:47

问题


I want my RTT app to be able to control when/whether we are receiving location updates, no matter from GPS or network. I can easily control GPS status with a GpsStatus.Listener but I cannot find the equivalent for network updates. For example, the event would fire if we enter an area with no network or wifi, or a timeout will occur like in the case covered by GpsStatus.GPS_EVENT_SATELLITE_STATUS

Codes snippets here:

    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:


Create a BroadCastReceiver that receives network updates.



来源:https://stackoverflow.com/questions/14198363/gpsstatus-listener-not-firing-on-network-status-changes

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!