我使用三星手机通过LocationManager API获取位置,如果禁用GPS,我无法获取位置,通过网络提供商我无法获取位置。
以下是代码,这在HTC&索尼甚至禁用了GPS而不是三星手机。
public Location getLocation() {
try {
mLocationManager = (LocationManager)this.getSystemService(Context.LOCATION_SERVICE);
// getting GPS status
isGPSEnabled = mLocationManager
.isProviderEnabled(LocationManager.GPS_PROVIDER);
// getting network status
isNetworkEnabled = mLocationManager
.isProviderEnabled(LocationManager.NETWORK_PROVIDER); //fails in samsung phone returns NULL
if (isGPSEnabled == false && isNetworkEnabled == false) {
// no network provider is enabled
} else {
this.canGetLocation = true;
if (isNetworkEnabled) {
mLocationManager.requestLocationUpdates(
LocationManager.NETWORK_PROVIDER,
MIN_TIME_BW_UPDATES,
MIN_DISTANCE_CHANGE_FOR_UPDATES, this);
if (mLocationManager != null) {
mLocation = mLocationManager
.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
}
}
// if GPS Enabled get lat/long using GPS Services
if (isGPSEnabled) {
if (mLocation == null) {
mLocationManager.requestLocationUpdates(
LocationManager.GPS_PROVIDER,
MIN_TIME_BW_UPDATES,
MIN_DISTANCE_CHANGE_FOR_UPDATES, this);
if (mLocationManager != null) {
mLocation = mLocationManager
.getLastKnownLocation(LocationManager.GPS_PROVIDER);
}
}
}
}
} catch (Exception e) {
e.printStackTrace();
}
return mLocation;
}
答案 0 :(得分:1)
此行为是三星手机独有的。 HTC你不会遇到这个问题。
您必须启动locationManager,因为默认情况下它会查看google地图缓存,而不关心缓存对于当前位置信息的陈旧程度。这样做,启动您的应用程序,形成一个不同的GPS位置,并注意它仍然显示旧的位置。现在启动谷歌地图,并重新启动您的应用程序,您的位置将发生变化。要以编程方式启动应用程序以获取“当前位置”,请在致电getLastKnownLocation
YOUR_APPLICATION_CONTEXT.getLocationManager().requestLocationUpdates(
LocationManager.NETWORK_PROVIDER, 0, 0, new LocationListener() {
@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
}
@Override
public void onProviderEnabled(String provider) {
}
@Override
public void onProviderDisabled(String provider) {
}
@Override
public void onLocationChanged(final Location location) {
}
});