Android Dev Guide:RequestingLocationUpdatesのこのセクションを確認してください。基本的に、位置情報の更新を取得するにはリスナーを登録する必要があります。次に、ネットワーク位置情報プロバイダーでそれらの更新を取得することをAndroidに通知します。
// 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);
これで、getLastKnownLocation()を使用して最後の既知の場所を取得したり、LocationListenerでonLocationChanged()を使用したりできるようになります。