問題描述
android ‑ 即使沒有可用的互聯網,也會調用 onLocationChanged (android ‑ onLocationChanged is called even when there is not internet available)
我只是想知道這個,因為它沒有意義。
我連接了一個 GoogleApiClient
,之後我使用
LocationServices.FusedLocationApi.requestLocationUpdates(
googleApiClient, mLocationRequest, this);
現在,奇怪的是 ‑ 即使我將手機置於飛行模式,onLocationChanged(Location location)
也會被調用!沒有互聯網,我仍然可以獲得位置更新。這怎麼可能?這些更新是否可靠且準確?
參考解法
方法 1:
In regular situations, the location is calculated based on one or several sensors. The data include:
- Cellular networks.
- Surrounding Wi‑Fi networks.
- Surrounding bluetooth beacons.
- GPS.
The first 3 methods require radios that can both listen and broadcast radio signals, however the GPS only listens for signals broadcasted by satellites, as it does not broadcasts anything. When you activate the airplane mode, you are basically telling to the device to stop broadcasting any kind of signal (in order to avoid interferences with the aircraft), however as the GPS chip does not broadcast anything, it can be expected to keep listening for satelite signals and thus, reporting you back the device current location.
You can programatically detect the accuracy of the location object by calling:
location.getAccuracy();
Which is the accuracy of the location in meters.
Edit: Let me add some important points that might be interesting for you:
- GPS first fix is really slow if it does not have assistance from any other radio. It can take minutes until you see the first location.
- GPS needs a clear view of the sky in order to receive signals. So it won't work indoors.
- GPS is one of the more power consuming ways to get location, compared with other techniques.
方法 2:
In a case where there are no internet or any update yet, it returns your last know location. so even in flight mode, you will probably still get it, it will only change once there is a new location update which will or may require internet before getting
(by Ofek Agmon、tomacco、Muyide Ibukun)