At the same time, more than one service provider

I have problems with location systems. I have a service that implements a locationlistener. I want to get the best location using the network, when possible, gps if the network is not accurate enough (accuracy above 300 mt). The problem is this. I need a place (more precisely, if possible, inaccurately different) every 5 minutes. I start with:

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 an accuracy of more than 300 meters, I want to go to the gps location system.

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

 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); } 

the system stops waiting for gpsupdate, and if I enter the nearest room, it can remain without location updates for several hours, ignoring the timeupdate. Is there a way to tell the locationprovider to switch to the network if gps doesn't indicate the location in “x” seconds? Or how to understand when gps is not localized?

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

Any suggestion?

+4
source share
1 answer

I wrote the answer to your question, but then I realized that my anser would not be able to compete with this excellent Android Dev Guide resource . This explains how, fans, the pros and cons of location listeners.

If you are following the GPS road in Android, this is a must read. It answers your above question and more. Believe me, it is worth your while.

It also presents samples with each SDK that implement useful examples. I am sure this was an example of location.

+1
source

Source: https://habr.com/ru/post/1402416/


All Articles