0

I have already tried out the following code to get country code name

TelephonyManager tm = (TelephonyManager)getActivity().getSystemService(getActivity().TELEPHONY_SERVICE);
        String countryCodeValue = tm.getNetworkCountryIso();

But I guess this does not work if I want to get current country code in which the user is.

For example if user travels from one country to another I want to get the country code in which he is in.

I have a solution to get user's current country code but I am stuck:

  1. I fetch users current location.
  2. Get the user address using its current location from this I can get the country name.

But is there any way I can get the country code from its country name in android ?

Also please let me me know if there is more efficient way.

Thanks :)

ik024
  • 3,566
  • 7
  • 38
  • 61
  • See answer of this question: http://stackoverflow.com/questions/5402253/getting-telephone-country-code-with-android – Nicolas R Aug 11 '14 at 10:57
  • You can get user's location's latitude and longitude. And then use a service (which is find country by latitude and longitude). – okarakose Aug 11 '14 at 10:58
  • Hi, what is the problem with this solution exactly? Is it tied to your purchase location or something like that? – Karthik T Sep 29 '14 at 11:24

2 Answers2

0

You can get the ISO 3166-1 alpha-2 (US, GB, etc) by getResources().getConfiguration().locale.toString().split("_")[1].

mach
  • 8,315
  • 3
  • 33
  • 51
  • No, this won't work as it will only show the country code of the locale that the mobile device has been set with, and it only changes if the user changes the system locale of the mobile device. What Droider asks is different - he wants dynamic derivation of country code based on user's current location, either obtained via GPS (e.g. get longitude and latitude and do a reverse lookup) or other means. – ChuongPham Aug 11 '14 at 11:29
0

For anyone coming across this question in the future. Use SmartLocation

Kotlin:

SmartLocation.with(this).location(LocationGooglePlayServicesProvider()).oneFix().start {
            SmartLocation.with(this).geocoding().reverse(it, OnReverseGeocodingListener { location, mutableList ->
                val countryCode = mutableList[0].countryCode
            })
        }
Ty Lertwichaiworawit
  • 2,950
  • 2
  • 23
  • 42