1
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.

santBart
  • 2,466
  • 9
  • 43
  • 66
  • Read related questions and note this is a bug http://stackoverflow.com/questions/3619924/android-geocoder-why-do-i-get-the-service-is-not-available http://stackoverflow.com/questions/4761052/why-is-android-geocoder-throwing-a-service-not-available-exception – Marek Sebera Jan 08 '12 at 20:55
  • Is there any chance to get address from location? – santBart Jan 08 '12 at 20:57

1 Answers1

5

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.

The Geocoder class requires a backend service that is not included in the core android framework

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 a getFromLocationName() call I still got an IOException: Service Not Available

It seems the most reliable way to determine if a backend is present is to just make the getFromLocation... call and see if your addresses field is null or if the List is empty. There is a bit of ambiguity in that these methods may also return a null or empty list if there are simply no matches found.

Tony Chan
  • 8,017
  • 4
  • 50
  • 49