4

I have some problems with location systems. I have a service that implements locationlistener. I want to get the best location using network when possible, gps if network is not enough accurate (accuracy greater than 300mt). The problem is this. I need location (accurate if possible, inaccuarte otherways) every 5 minutes. I start with a :

    LocationManager lm=(LocationManager)getApplicationContext().getSystemService(LOCATION_SERVICE);
    Criteria criteria = new Criteria();
    criteria.setAccuracy(Criteria.ACCURACY_COARSE);
    criteria.setAltitudeRequired(false);
    criteria.setBearingRequired(false);        
    String provider=lm.getBestProvider(criteria, true);
    if(provider!=null){


    lm.requestLocationUpdates( provider,5*60*1000,0,this);

In "onLocationChanged" i listen to locations and when i get a location with accuracy greater than 300mt, i want to change to gps location system.

If I remove allupdates and then request for gps updates, like this:

lm.removeUpdates((android.location.LocationListener) this);
Criteria criteria = new Criteria();
    criteria.setAccuracy(Criteria.ACCURACY_FINE);
    criteria.setAltitudeRequired(false);
    criteria.setBearingRequired(false);        
    String provider=lm.getBestProvider(criteria, true);
    if(provider!=null){
    lm.requestLocationUpdates( provider,5*60*1000,0,this);
    }

system stops waiting for gpsupdate, and if i'm in a close room it can stay without location updates for hours, ignoring timeupdate indications. Is there a way to tell locationprovider to switch to network if gps is not giving a location in "x" seconds? or how to understand when gps is not localizing?

or if i requestlocationupdates from 2 providers at same time (network and gps), can be a problem?

Any suggestion?

Rabarama
  • 73
  • 1
  • 5

1 Answers1

1

I was writing an answer specific to your question, but then I realized there was no way my anser could compete with this excellent resource the Android dev guide provides. It explains the hows, the whys, the pros and the cons of location listeners.

If you're going down the GPS road in Android, it's a must-read. It answers your above question and many more. Trust me, it's worth your time.

There's also the samples provided with each SDK that implement useful examples. I'm pretty sure the was a location example.

salezica
  • 74,081
  • 25
  • 105
  • 166
  • Thanks for link. Now i changed all my service with alarms controlling update request from gps. I'm checking for wifi location, if it's not enough accurate, i check for gps and if i can't get any gpslocation in 60 secs, i remove locationupdates for gpslistener leaving networklistener on. But It's not working everything how i would like: when I ask for gpsupdate, gps icon correctly appears, but it stay fixed and seems to do nothing, and i don't receive location in 60 secs. After 60s, my service stop listening and icon disappear. Am I forgetting something to turn gps on and obtain a loc? – Rabarama Mar 20 '12 at 17:06
  • I would just leave the listeners on. You'll receive updates WHEN APPROPIATE. The interval, both in time and space, is meant to cut off unnecessary callbacks by setting a MINIMUM DISTANCE/TIME BETWEEN MEASUREMENTS BEFORE YOU ARE EVEN BOTHERED. Just trust the results that appear in the callback, and analyze their timestamps and metadata as the guide suggests. If you're not getting any new location after GPS locks, the information is probably not worth it -- I get new info instantly, unless I'm on wi-fi, which is already a very good fix. – salezica Mar 21 '12 at 01:09