Skip to content Skip to sidebar Skip to footer

Get Country Code Based On User's Current Location Android

I have already tried out the following code to get country code name TelephonyManager tm = (TelephonyManager)getActivity().getSystemService(getActivity().TELEPHONY_SERVICE);

Solution 1:

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

Solution 2:

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
            })
        }

Post a Comment for "Get Country Code Based On User's Current Location Android"