问题
We know there are two positioning mode in Android: GPS and network. If we use network, then Android can use WIFI or 2G/3G cell tower to position. Without GPS, we can simply use LocationManager.NETWORK_PROVIDER to get the location data. But how can we know the mode is WIFI or 2G/3G cell tower exactly? Android doesn't provide separate providers for WIFI and cell tower.
I have thought a method. But I am not sure whether it is correct. Please review and give comments:
- Check whether I can get a WIFI hotspot list. If nothing in the list, cell tower must be used.
- If there are some WIFI hotspots, and the accuracy is high (<=100m), then WIFI is probably used. If the accuracy is low, is still cell tower used?
In my understanding, the accuracy of WIFI positioning is relatively high. But what's the normal range of the accuracy for it? Another question, does Android use WIFI and cell tower at the same time? If it does, then in my application, I can think it use WIFI, not cell tower.
Thanks!
回答1:
This is correct, Android does not explicitely give a way to determine how the network location is computed. The principle of the network provider is to collect all the relevant data on the device, and then send it all to the Google servers, where your location is computed and sent back to you. I am not sure, but the network location might also use the accelerometer/gyro/compass data to improve the accuracy of the location, especially when you compute your location continuously. So I believe it can use simultaneously cell tower and Wifi info.
Your accuracy method should work in most cases, but is not 100% reliable.
Another possibility is to turn the wifi off, compute a first location fix, then turn it on again and give it another try. by comparing the two locations and their accuracy estimates, you can probably guess the influence of wifi on the location engine at your current location.
Out of curiosity, why do you need this for?
回答2:
Actually, there is a way to do this. It is not just documented well and thus might also change without notice. You can get the used location estimation method via the network location provider location update extra data.
Bundle extras = location.getExtras();
if (extras.containsKey("networkLocationType")) {
String type = extras.getString("networkLocationType");
if (type .equals("cell")) {
// Cell (2G/3G/LTE) is used.
} else if (type .equals("wifi")) {
// Wi-Fi is used.
}
}
来源:https://stackoverflow.com/questions/10631811/how-to-know-the-positioning-mode-is-wifi-or-2g-3g-cell-tower-in-android