BQuadra BQuadra - 4 months ago 7x
Android Question

Find user position with a timeout

I need to find the user position with a timeout, then I wrote a code like this

  1. Start a LocationListener

    GPSLocationListener = new LocationListener() {

    public void onLocationChanged(Location location) {

    public void onStatusChanged(String provider, int status, Bundle b) {

    public void onProviderEnabled(String s) {

    public void onProviderDisabled(String s) {

    _locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, minTime, minDistance, _GPSLocationListener);


  2. Setup a timer for the timeout

    _timerGPSLocation = new Timer();
    _timerGPSLocation.schedule( new TimerTask() {

    public void run() {

    }, (long)(timeout*1000) );

I think that doing this (trying to read coordinates and setup a timer with timeout) for many times can let the GPS contact some satellites and give me the right location.
There is a better way for doing this?
Calling _locationManager.removeUpdates on the timeout will remove all contacted satellites?


My goal is to read the GPS at regular intervals (5 minutes). I need also to set a timeout while try to get the location using GPS read. if no location is read after the timeout I need to stop the location listener. I've achieved this using the code liste here.

Now my question is if removing the LocationListener because it's go in timeout will cause the loss of the "acknowledgement" between the GPS and the satellite.


Scheduling a timer would work for what you're trying to do. Similarly you could use a handler and pass it a thread or a Looper object. As in

Handler h = new Handler();

h.postDelayed(Runnable r, long millis);

With that you could also post a runnable, i.e thread or looper at a time delay to cancel location updates.

EDIT: postDelayed is used for a runnable to run after a certain delay. postAtTime posts at a certain uptime of the program