thisisdee
thisisdee

Reputation: 878

Android: How to timeout GPS trying to find a lock?

Here are snippets of my code:

private class NoGpsLock implements Runnable {
    public void run() {
      if (gps == null) {
        Toast toast = Toast.makeText(getApplicationContext(), "Unable to find GPS lock", Toast.LENGTH_LONG);
        toast.show();
        if (locManager != null) {
          locManager.removeUpdates(locListener);
        }
      }
    }
  }

Called by

try {
  locManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, MIN_TIME, MIN_DIST, locListener);
} catch (Exception ex) {
}
Handler gpsHandler = new Handler();
gpsHandler.postDelayed(new NoGpsLock(), 15000); //15seconds

From what I understand, removeUpdates() is supposed to stop the GPS receiving updates. I tried this on the emulator and on 2 devices; on the emulator, it does stop the app from receiving further location changes. However, on the actual devices, the icon for "Receiving location data from GPS" keeps showing up and drains the battery (which I assume indicates that the app/phone keeps on looking for a location and unable to, since I'm indoors). How do I stop the GPS from trying to find a location?

EDIT: I want my app to stop trying to find location after a period of time, and then maybe restart the GPS again later.

Upvotes: 1

Views: 1557

Answers (1)

PVS
PVS

Reputation: 1168

removeUpdates(locationListener) is supposed to stop that blinking GPS icon. If it isn't doing that, the best suggestion I can offer is that maybe there are there are other listener instances still registered with your locationManager? A forgotten for-loop could have attached other listener instances. As a brute-force developer I'd do removeUpdates() multiple times to see if that has any effect.

Upvotes: 2

Related Questions