您要做的是使用LocationManager.NETWORK_PROVIDER而不是获取职位LocationManager.GPS_PROVIDER。在NETWORK_PROVIDER将解决在GSM或WiFi,这永远可用。显然,关闭wifi后,将使用GSM。请记住,使用小区网络的精度基本上可以达到500m。
http://developer.android.com/guide/topics/location/obtaining-user-location.html提供了一些非常有用的信息和示例代码。
完成中的大多数代码后OnCreate(),添加以下代码:
// 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);
您也可以让您的活动实现LocationListener该类,从而onLocationChanged()在您的活动中实现。