Search code examples
androidlocationlistener

LocationListener timeout


I am currently using a timer to determine if a location listener has timed out ? The problem is that the gps remains on. I don't know why , is there a method i can override when the locationlistener times out or a more elegant method ?


Solution

  • Consider adding a GPS status listener to your location manager. The status listener is informed when the GPS starts, stops, receives a first fix, or the satellite status (no. of visible satellites, you need at least 4 for a fix) changes.

    The listener may look like this:

    class GpsStatusListener implements GpsStatus.Listener {
    
        @Override
        public void onGpsStatusChanged(final int event) {
            switch( event ) {
                            // ...
                break;
            case GpsStatus.GPS_EVENT_STOPPED:
                            // ...
                break;
            case GpsStatus.GPS_EVENT_FIRST_FIX:
                            // ...
                break;
            case GpsStatus.GPS_EVENT_SATELLITE_STATUS:
                            // ...
                break;
            }
        }
    }
    

    It is added as follows:

    lm.addGpsStatusListener(new GpsStatusListener());
    

    You don't need to remove the location listener when the GPS status changes.

    You also can get additional information from the location manager by overriding one of the following methods:

    public void onStatusChanged(final String provider, final int status, final Bundle extras) {
        switch( status ) {
        case LocationProvider.AVAILABLE:
                    // ...
            break;
        case LocationProvider.OUT_OF_SERVICE:
                    // ...
            break;
        case LocationProvider.TEMPORARILY_UNAVAILABLE:
                    // ...
            break;
        }
    }
    
    @Override
    public void onProviderEnabled(final String provider) {
                    // ...
    }
    
    @Override
    public void onProviderDisabled(final String provider) {
                    // ...
    }