3

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

  1. Start a LocationListener

    GPSLocationListener = new LocationListener() {
    
    @Override
    public void onLocationChanged(Location location) {
             _timerGPSLocation.cancel();
    }
    
    @Override
    public void onStatusChanged(String provider, int status, Bundle b) {
    }
    
    @Override
    public void onProviderEnabled(String s) {
    }
    
    @Override
    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() {
    
    @Override
    public void run() {
        _locationManager.removeUpdates(_GPSLocationListener);
    }
    
    }, (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?

EDIT:

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.

roccocullo
  • 178
  • 11
BQuadra
  • 789
  • 2
  • 11
  • 20
  • Are you trying to get GPS updates at regular intervals? if not then I am slightly confused – dannyRods Jan 19 '13 at 00:52
  • I try to read the GPS at regular intervals (5 minutes), but want to have a timeout on the GPS read (30secs), if no location is read after the timeout want to stop the location listener. – BQuadra Jan 19 '13 at 02:23
  • I would take advantage of the onStatusChanged method in the location listener then. That method is called if something like the GPS is unavailable, if it will be right back up, or if it is available. It will be called once you start requesting updates so if you put an if statement in there you can tell if its available. the page for that is [this](http://developer.android.com/reference/android/location/LocationListener.html#onStatusChanged%28java.lang.String,%20int,%20android.os.Bundle%29) – dannyRods Jan 20 '13 at 18:17

1 Answers1

1

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

dannyRods
  • 552
  • 6
  • 23
  • Note also that its a handler from android.os not from java.util.logging – dannyRods Jan 19 '13 at 01:11
  • Thanks for the tips. But I want to known if adding/removing the LocationListener while it's go in timeout will cause the loss of the "acknowledgement" between the GPS and the satellite. – BQuadra Jan 19 '13 at 02:25
  • Well yeah. Removing the listener means that you are not going to receive any updates from the GPS because you no longer need them. But as soon as you call request location updates again the "acknowledgement" will once again be there. – dannyRods Jan 20 '13 at 18:06
  • Ok, I want to liten this: "But as soon as you call request location updates again the "acknowledgement" will once again be there.". Thanks. – BQuadra Jan 20 '13 at 18:47
  • I'm glad I could help, but I'm not sure if I know what you mean by "OK, I want to liten this" – dannyRods Jan 20 '13 at 19:18
  • Do you mean you want to hear more about it? like how to do it? – dannyRods Jan 23 '13 at 00:53
  • Yes, I want to hear more about this argument, about the optimizing and strong way for get the position with GPS having a timeout (when getting the position take more time then the timeout my function has to stop and return null). Thanks a lot! – BQuadra Jan 23 '13 at 21:47