7

When my app runs, I want to detect the current location's country name and do some stuff for that specific country name, I can easily do it using geocoder, gps. But I want to get it from Locale or if anything more is available.

Reyjohn
  • 2,654
  • 9
  • 37
  • 63
  • *"do some stuff"* What stuff specifically? – Andrew Thompson May 25 '12 at 13:48
  • I want to make a currency converter, So I need to detect the location from where the app is launched and set the default currency of that country – Reyjohn May 25 '12 at 13:51
  • Can you use the Google Location Services API without the GPS? Or do you want to avoid that as well? – psubsee2003 May 25 '12 at 13:52
  • Actually I want to avoid anything that uses internet connection, is there a way using timezone or something? – Reyjohn May 25 '12 at 13:56
  • Time Zone is not ideal. If you just use the UTC offset, you'll only know approximate longitude, but that won't help at all. The time zone name might be more exact, but even then too many countries share time zones. You'll never be sure you have the correct one. – psubsee2003 May 25 '12 at 14:04
  • I have a list of all currencies of all countries, then how can i get the current country to set the default currency? – Reyjohn May 25 '12 at 14:08

3 Answers3

16

Use this link http://ip-api.com/json ,this will provide all the information as json. From this json you can get the country easily. This site works using your current IP,it automatically detects the IP and sendback details.

Docs http://ip-api.com/docs/api:json Hope it helps.

This is what I got.

{
"as": "AS55410 C48 Okhla Industrial Estate, New Delhi-110020",
"city": "Kochi",
"country": "India",
"countryCode": "IN",
"isp": "Vodafone India",
"lat": 9.9667,
"lon": 76.2333,
"org": "Vodafone India",
"query": "123.63.81.162",
"region": "KL",
"regionName": "Kerala",
"status": "success",
"timezone": "Asia/Kolkata",
"zip": ""

}

shine_joseph
  • 2,922
  • 4
  • 22
  • 44
  • 2
    but it has limitations. their system will automatically ban any IP addresses doing over 150 requests per minute. – Ram Mandal Jul 15 '16 at 09:44
5

I dont know if Android API allowes you to check the Provider Code of the associated GSM Network. If this is possible you could use this Code an check i.e. against this List : http://www.techgsm.com/page/gsm-provider-codes/network-provider-identify-codes.html to find out in which country the Cellphone is.

spot
  • 74
  • 1
  • 1
    +1 That's brilliant... never even crossed my mind. The Provider code is also called the MCC. Found a question elsewhere on SO that might help: http://stackoverflow.com/q/890366/250725 ... although, probably won't work in airplane mode. – psubsee2003 May 25 '12 at 14:11
1

You can (and should) use Android's country detector service. The service consists of three methods, all defined in their .aidl, which enable you to get the country code, register a listener, or remove it. Specifically, you want the first - Country detectCountry(); which returns an android.location.Country object (frameworks/base/location/java/android/location/Country.java in the AOSP). I'm assuming you know how to call methods programmatically (you'll need getSystemService ("country_detector")).

To test from the command line, try:

shell@flounder:/ $ service call country_detector 1
Result: Parcel(
  0x00000000: 00000000 00000001 00000002 00530055 '............U.S.'
  0x00000010: 00000000 00000003 1d809dda 00000000 '................')

The object serialization is basically nothing more than the 2 letter country code (here: US), the source (here, 3, implying this was gleaned from the Locale), and a timestamp (1d809dda...) which really isn't that important. You don't have to worry about serialization since the object is Parcelable.

Technologeeks
  • 7,674
  • 25
  • 36