Reputation: 73
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?
Upvotes: 4
Views: 3987
Reputation: 76929
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.
Upvotes: 1