這是不壞所需的屬性。關於這個問題的官方Android SDK其實很不錯。 Official Google Documentation on it
基本上,它使用的接口運行功能,因爲每種情況都會出現。因此,只要位置根據任何參數觸發關閉而改變,就會調用onLocationChanged。此外,我會建議只是玩不同的提供商類型
最難的部分是距離的一部分。有方程式用來計算經度和緯度的距離。
從文檔:
// Acquire a reference to the system Location Manager
LocationManager locationManager = (LocationManager) this.getSystemService(Context.LOCATION_SERVICE);
// Define a listener that responds to location updates
LocationListener locationListener = new LocationListener() {
public void onLocationChanged(Location location) {
// Called when a new location is found by the network location provider.
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
locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0, locationListener);