我使用以下代码获取位置。它在Android 4.0.4中工作正常,但在4.1.1中没有。谷歌搜索了很多,但没有找到任何解决方案。请帮助我提前谢谢。
mLocationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
mLocListener = new UserLocationListener(getApplicationContext());
if (mLocationManager
.isProviderEnabled(LocationManager.NETWORK_PROVIDER)) {
mLocationManager.requestLocationUpdates(
LocationManager.NETWORK_PROVIDER, 1, 0, mLocListener);
} else if (mLocationManager
.isProviderEnabled(LocationManager.GPS_PROVIDER)) {
mLocationManager.requestLocationUpdates(
LocationManager.GPS_PROVIDER, 1, 0, mLocListener);
}
public class UserLocationListener implements LocationListener {
Context mContext;
String mCurrLocation = null;
double latitude, longitude;
String locale;
public String getLocale() {
return locale;
}
public String getmCurrLocation() {
return mCurrLocation;
}
public double getLatitude() {
return latitude;
}
public double getLongitude() {
return longitude;
}
public UserLocationListener(Context _mContext) {
mContext = _mContext;
}
@Override
public void onLocationChanged(Location location) {
Geocoder gcd = new Geocoder(mContext, Locale.getDefault());
List<Address> addresses = null;
try {
addresses = gcd.getFromLocation(location.getLatitude(),
location.getLongitude(), 1);
longitude = location.getLongitude();
latitude = location.getLatitude();
locale = Locale.getDefault().getCountry();
if (addresses.size() > 0) {
mCurrLocation = formatAddress(addresses.get(0));
} else {
Log.e("LocationListener",
"Geocoder backend service not present");
}
} catch (IOException e) {
} catch (NullPointerException e) {
}
}
/**
* Formats the Address to a meaningful String representation
*
* @param address
* @return String
*/
private String formatAddress(Address address) {
String addr = "";
StringBuilder sb = new StringBuilder();
if (address.getAddressLine(0) != null) {
sb.append(address.getAddressLine(0));
} else if (address.getSubThoroughfare() != null) {
sb.append(address.getSubThoroughfare());
} else if (address.getSubThoroughfare() != null) {
sb.append(address.getSubThoroughfare());
} else if (address.getThoroughfare() != null) {
sb.append(address.getThoroughfare());
} else if (address.getSubLocality() != null) {
sb.append(address.getSubLocality() + ",");
} else if (address.getPremises() != null) {
sb.append(address.getPremises() + ",");
} else if (address.getLocality() != null) {
sb.append(address.getLocality() + ",");
} else if (address.getSubAdminArea() != null) {
sb.append(address.getSubAdminArea() + ",");
} else if (address.getAdminArea() != null) {
sb.append(address.getAdminArea() + ",");
} else if (address.getPostalCode() != null) {
sb.append(address.getPostalCode() + ",");
} else if (address.getCountryName() != null)
sb.append(address.getCountryName() + ",");
addr = sb.toString();
return addr;
}
@Override
public void onProviderDisabled(String provider) {
// TODO Auto-generated method stub
}
@Override
public void onProviderEnabled(String provider) {
// TODO Auto-generated method stub
}
@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
// TODO Auto-generated method stub
}
}