2

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 ?

Lucifer
  • 29,392
  • 25
  • 90
  • 143
opc0de
  • 11,557
  • 14
  • 94
  • 187

2 Answers2

2

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) {
                // ...
}
Stefan
  • 4,645
  • 1
  • 19
  • 35
  • When exactly I get that cases : case LocationProvider.AVAILABLE , case LocationProvider.OUT_OF_SERVICE , case LocationProvider.TEMPORARILY_UNAVAILABLE , Because I belive that might helps me with my issue : http://stackoverflow.com/questions/15747543/locationlistener-of-network-provider-is-enabled-but-onlocationchanged-is-never – Guilherme Gregores May 24 '13 at 14:33
  • I Log each case and generate a report of each one in the onStatusChanged of the LocationListener (NETWORK_PROVIDER), But it is not happening – Guilherme Gregores May 24 '13 at 14:45
  • THat will depend on your hardware. I sometimes get the temporarily unavailable signal if the GPS cannot see the satellites anymore (e.g. when entering a building). I never saw the out of service signal yet. – Stefan May 28 '13 at 08:20
  • But, you have already gotten the others status like: AVAILABLE and TEMPORARILY_UNAVAILABLE? – Guilherme Gregores May 28 '13 at 19:52
  • Yes. Note you won't (at least not with my hardware) get an available signal when you switch the GPS on. It needs to be unaivailable before. And I don't get the temp. unavailable every time the GPS looses contact with the satellites. If you want to monitor that use first example (satellite count). – Stefan May 29 '13 at 06:47
  • I think this kind of status just appear for android 2.2 and below, because I tried to use it with some device here like: Galaxy Y 2.3 LG optimus L5 4.0 and galaxy S3 4.1.2 , but without successes, then I have tried the Samsung Galaxy Tab 2.2 and it works pretty well, do you know something about it??if have another way to get this status for android 2.3+ – Guilherme Gregores May 29 '13 at 18:44
0

you should remove location listener when time-out has occurred or you have got the current location.