More than one location provider at same time

旧街凉风 提交于 2019-12-22 08:37:59

问题


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?


回答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.



来源:https://stackoverflow.com/questions/9785768/more-than-one-location-provider-at-same-time

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!