在networking和GPS提供商之间切换

我想实现一个locationListener,它将根据可用性在networking和GPS提供商之间切换。

例如,如果GPS没有启用,我希望它使用networking,但一旦GPS打开,那么我希望它停止侦听来自networking的位置更新,并开始从GPS收听。

同样,我希望它在GPSclosures后立即开始监听来自networking的更新。

那可能吗?


Subquestion

GPS在提供定位方面与networking一样快吗?


当然,您只需获取networking和GPS的提供商,并将其传递给locationManager.requestLocationUpdates()

当您想要停止侦听某个提供者时,使用您在locationManager.requestLocationUpdates()指定的侦听器对象调用locationManager.requestLocationUpdates()

networking:

 Criteria crit = new Criteria(); crit.setPowerRequirement(Criteria.POWER_LOW); crit.setAccuracy(Criteria.ACCURACY_COARSE); String provider = locationManager.getBestProvider(crit, false); 

全球定位系统:

 Criteria crit2 = new Criteria(); crit2.setAccuracy(Criteria.ACCURACY_FINE); provider2 = locationManager.getBestProvider(crit2, false); 

您可以使用LocationManager.isProviderEnabled()doc来查看相应的提供程序是否已启用/禁用。 LocationManager文档中提供了更多信息。

全球定位系统通常比networking慢得多,因为你必须find3个以上的遥远的卫星等。

我正在使用这个

 LocationManager locationManager; LocationListener locationListener; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); locationManager = (LocationManager) this .getSystemService(Context.LOCATION_SERVICE); String locationProvider = LocationManager.NETWORK_PROVIDER; Location lastKnownLocation = locationManager .getLastKnownLocation(locationProvider); if (lastKnownLocation == null) { locationProvider = LocationManager.GPS_PROVIDER; lastKnownLocation = locationManager .getLastKnownLocation(locationProvider); } if (lastKnownLocation != null) { makeUseOfNewLocation(lastKnownLocation); } locationListener = new LocationListener() { public void onLocationChanged(Location location) { makeUseOfNewLocation(location); } public void onStatusChanged(String provider, int status, Bundle extras) { } public void onProviderEnabled(String provider) { } public void onProviderDisabled(String provider) { } }; // Register the listener with the Location Manager to receive location // updates if (locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER)) { locationManager.requestLocationUpdates( LocationManager.GPS_PROVIDER, 0, 0, locationListener); } else { locationManager.requestLocationUpdates( LocationManager.NETWORK_PROVIDER, 0, 0, locationListener); } }