查看 Android 开发指南中的此部分:请求位置更新。基本上,您需要注册一个侦听器以获取位置更新,然后告诉 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() 获取最后一个已知位置。