Geocoder geocoder = new Geocoder(getApplicationContext(), Locale.ENGLISH);
try {
List<Address> addresses = geocoder.getFromLocation(lat,lon, 1); <--Exception
This code cause exception Service not Available.
Can anyone help me? I’ve read that it is a bug for A 2.2, api 10 is A 2.3.3 so thoretically it should work.
ANSWER:
First if you are using the emulator, try setting the “target” for the AVD device to one of the “Google APIs” instead of the basic “Android” sdk. So if you want to target 2.3.3 use “Google APIs (Google Inc.) – API Level 10” and not “Android 2.3.3 – API Level 10”.
BACKGROUND INFO
The Google APIs addon includes a backend service implementation(search “geocoder backend”) that is required for the
Geocoder.getFromLocation...methods to work.That is why your AVD must target Google APIs for these methods to work. If that doesn’t work try testing on a real device. Other than a possible bug, the reason for getting this exception is either a problem with the internet/lack of connection, or more likely there is no backend service on that device.
Supposedly most devices that include Google services and apps like Maps, etc will have a backend implemented. I don’t know how accurate this is.
There is also a method starting with API9,
Geocoder.isPresent()that will return true if there is a backend implementation present. However, I have found this to be unreliable since testing it on an Amazon Kindle Fire returned true, but when making agetFromLocationName()call I still got anIOException: Service Not AvailableIt seems the most reliable way to determine if a backend is present is to just make the
getFromLocation...call and see if youraddressesfield isnullor if theListis empty. There is a bit of ambiguity in that these methods may also return anullor empty list if there are simply no matches found.