网络和GPS提供商之间切换 [英] Switching between network and GPS provider
问题描述
我想实现一个LocationListener的,将根据可用性网络和GPS提供商之间进行切换。
I want to implement a locationListener which will switch between network and GPS providers based on availability.
例如,如果GPS未启用,我希望它使用网络,但只要GPS是那么我希望它停止监听来自网络位置更新,并开始从GPS的听着。
For example if GPS is not enabled I want it to use network but as soon as GPS is on then I want it to stop listening for location updates from network and start listening from GPS.
同样地,我希望它开始监听来自网络的更新,一旦GPS被关闭。
Similarly I want it to start listening for updates from network as soon as GPS is switched off.
这可能吗?
Subquestion
时的GPS一样快,网络在提供位置锁定?
Is GPS as fast as network in providing a location fix?
推荐答案
当然,你只得到了供应商的网络和GPS,并通过任何你想 locationManager.requestLocationUpdates()
。
Sure, you just get the providers for the network and GPS and pass whichever you want to locationManager.requestLocationUpdates()
.
当你想停止收听到一定的提供商,呼叫 locationManager.removeUpdates()
与您 locationManager.requestLocationUpdates指定的侦听器对象( )
。
When you want to stop listening to a certain provider, call locationManager.removeUpdates()
with the listener object you specified in locationManager.requestLocationUpdates()
.
网络:
Criteria crit = new Criteria();
crit.setPowerRequirement(Criteria.POWER_LOW);
crit.setAccuracy(Criteria.ACCURACY_COARSE);
String provider = locationManager.getBestProvider(crit, false);
GPS:
Criteria crit2 = new Criteria();
crit2.setAccuracy(Criteria.ACCURACY_FINE);
provider2 = locationManager.getBestProvider(crit2, false);
您可以使用<一个href="http://developer.android.com/reference/android/location/LocationManager.html#isProviderEnabled%28java.lang.String%29">LocationManager.isProviderEnabled()文档,查看相应的供应商已启用/禁用。有一个在LocationManager文档提供更多的信息。
You can use LocationManager.isProviderEnabled() doc to see if the appropriate provider is enabled/disabled. There's more info available in the LocationManager docs.
GPS是不是网络通常要慢得多,因为你必须找到3+遥远的卫星等。
GPS is usually much slower than network since you have to find 3+ far-away satellites, etc.
这篇关于网络和GPS提供商之间切换的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!