问题
Here's the scenario. I need to prioritize getting GPS from satellite. However if indoor, there will be no new locations received. What I did was if there's no new location received after 2 minutes, then I will switch to network GPS. However, what if the user went outside, the app wouldn't know because it's already listening from network GPS, it will not receive locations from satellite anymore.
so it would be better if I could listen to new location updates, weather be from satellite or from network. though better if I can prioritize the ones from satellite.
I found this question and the chosen answer really helped a lot: Android LocationListener Switching from GPS to Network
But there was just a part that confused me. He said: You setup the same LocationListener for both providers. To indentify from where you are receiving the location you need to test it like this:
public void onLocationChanged(Location fix) {
if(fix.getProvider().equals(LocationManager.GPS_PROVIDER)){
//here Gps
} else if(fix.getProvider().equals(LocationManager.NETWORK_PROVIDER)){
//here Network
}
}
so if that's the case, how do I disable receiving updates from only the network provider? because if I use a single listener which listens to both gps and network, then when I do removeUpdates(thatListener)
, then that listener will not receive any (satellite or network) gps updates anymore.
but if he's saying that I'll make 2 listeners which are just exactly the same (except one is for satellite one is for network), then I wouldn't need to check which provider gave the location because each listener will have its own onLocationChanged
method. Am I missing out on something? I don't have enough reputation so I can't comment directly to his answer
also, how long does satellite GPS usually take before being able to receive location updates?
Thank you very much! :)
回答1:
I guess the new Fused location provider is made to suit your needs. Let Google do this work for you and always take the best Provider present. You'll need Google Play Service installed for that, but thats mostly no problem. Setup the Locationservice has become much easier with this. See here for a nice example.
来源:https://stackoverflow.com/questions/25891976/how-do-i-remove-only-the-network-provider-if-my-listener-listens-for-both-networ