2011-03-31 59 views
10

我想實現一個locationListener,它將根據可用性在網絡和GPS提供商之間切換。在網絡和GPS提供商之間切換

例如,如果GPS未啓用我希望它使用網絡,但一旦GPS打開,我希望它停止偵聽來自網絡的位置更新並開始從GPS偵聽。

同樣,我希望它在GPS關閉後立即開始收聽來自網絡的更新。

這可能嗎?


Subquestion

是GPS一樣快,網絡中提供位置定位?


回答

9

當然,你只得到了網絡和GPS的供應商,並通過無論你想locationManager.requestLocationUpdates()

當您想停止收聽某個提供商時,請使用您在locationManager.requestLocationUpdates()中指定的監聽器對象調用locationManager.removeUpdates()

網:

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); 

您可以使用LocationManager.isProviderEnabled() doc,看看是否合適的供應商被啓用/禁用。 LocationManager文檔中提供了更多信息。

GPS通常比網絡慢得多,因爲你必須要找到3+遙遠的衛星等

+1

是的,但是當我初始化的LocationManager請求位置更新它只會從我傳遞的提供者那裏偵聽,即'locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER,minTime,minDistance,listener)'只會偵聽來自網絡的更新,並且onProviderEnabled將永遠不會被調用GPS! – mixkat 2011-03-31 20:07:38

+0

有一個'LocationManager.addGpsStatusListener()'方法。當它告訴你一些改變時,按照相應的行動。 – 2011-03-31 20:18:13

+0

由於某種原因,無法使用此功能!儘管我將gpsStatusListener添加到了locationManager,但onGpsStatusChanged()永遠不會被調用! – mixkat 2011-03-31 20:59:35

1

我用這

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); 
    } 
} 
+0

這並不夠靈活..最好是兩者兼而有之,並決定哪個更好 – Vlad 2013-08-12 14:20:35

+0

@Vlad你怎麼能決定? – Hector 2013-09-12 22:35:07

+1

@Hector閱讀此:http://developer.android.com/guide/topics/location/strategies.html – Vlad 2013-09-15 19:46:29

相關問題