Through nearly all problems/answers here and on the web, the best way for current location is as follows:
if ( mLocationManager==null )
mLocationManager = (LocationManager) getSystemService(LOCATION_SERVICE);
Location l;
for ( String provider : mLocationManager.getAllProviders() ){
mLocationManager.requestLocationUpdates(provider, 1000, 1, locationListener);
l = mLocationManager.getLastKnownLocation(provider);
if ( isBetterLocation(l, currentBestLocation) )
currentBestLocation = l;
}
normally it works fine on Emulator and devices, but as noted by some guys, Android 4.0+ this code gives an exception:
09-06 22:46:16.163: ERROR/AndroidRuntime(351): Caused by: java.lang.IllegalArgumentException: provider=network
09-06 22:46:16.163: ERROR/AndroidRuntime(351): at android.os.Parcel.readException(Parcel.java:1325)
09-06 22:46:16.163: ERROR/AndroidRuntime(351): at android.os.Parcel.readException(Parcel.java:1275)
09-06 22:46:16.163: ERROR/AndroidRuntime(351): at android.location.ILocationManager$Stub$Proxy.requestLocationUpdates(ILocationManager.java:646)
09-06 22:46:16.163: ERROR/AndroidRuntime(351): at android.location.LocationManager._requestLocationUpdates(LocationManager.java:582)
09-06 22:46:16.163: ERROR/AndroidRuntime(351): at android.location.LocationManager.requestLocationUpdates(LocationManager.java:446)
But other apps on the same device can work properly when there IS only WIFI/3G coverage(GPS not working indoors).
So my question is: How to retrieve current location inside a building?
PS: waiting for an Android image update is not acceptable, since other apps work properly on the same device. There MUST be a way to get the current location on the Android image with bug in LocationManager.
Kindest regards.
EDIT:
This time I tweaked my code as simple as possible(in the onResume):
if ( mLocationManager==null )
mLocationManager = (LocationManager) getSystemService(LOCATION_SERVICE);
for ( String provider : mLocationManager.getAllProviders() )
mLocationManager.requestLocationUpdates(provider, 0, 0, locationListener);
and in the locationListener.onLocationChanged:
public void onLocationChanged(Location location) {
if (isBetterLocation(location, currentBestLocation)) {
currentBestLocation = location;
...make use of this location
}
}
Only passive and gps providers are available, no network provider can be obtained on my XT928, while on the other phone it shows up.
EDIT 2: at last I achieved this by turning to a third-party library named as Baidu Location Library which also needs Access Key application. It works great.