Home » Android » android – How to retrieve the CountryCode / ISO?

android – How to retrieve the CountryCode / ISO?

Posted by: admin June 15, 2020 Leave a comment

Questions:

Our intention is to filter the content based on location without requesting Location permission from the Android device,

In-order to meet this requirement, we have tried the following options and still its not reliable

  1. Telephony Manager

it wont work without a Sim card, again say if the user from USA travels india now the below snippets returns US not India

var telephonyManager = context?.getSystemService(Context.TELEPHONY_SERVICE) as TelephonyManager
val countryCodeValue = telephonyManager.networkCountryIso
  1. Locale

Locale returns US or UK by default even if they are in India, usually user’s Locale would be in English by default

 Locale.getDefault().getCountry()
  1. GPS

We doesn’t want the Location permission to be requested

  1. Retrieving the Country details form the logged-in Google account

Reply from Google – They dont have any dedicated API to retrieve countryCode of an user
Note:
iOS has a dedicated API which tell the CountryCode of the user, but Android doesn’t

  1. IP address

We tried to get the IP of the Android device (from several IP fusion websites), there are following two cases

  • User connected to WiFi – we are able to get the countryCode (WIFI IP
    : 64.134.234.17)
  • User connected to Mobile Data – we are getting the wrong countryCode
    even we are in USA, it says that IP is from JAPAN (MOBILE NETWORK
    IP: 210.253.218.106)

Note:
Even Netflix determine the country during the launch, without requesting the Location permission

How to&Answers:

I think you have only two possibilities…or ask permission…or without asking permission you have to ask the user to insert an address (also only the city) and retrive the country code by mean of Google Geocoding Service

Answer:

I agreed the above comments. You can’t get the precise country by anyways other than there GPS location.
You can read this one hope you will get some idea about

Regarding your query how netflix is using it

https://help.netflix.com/en/node/26100

Brief

1.Disable any proxies (Means they are checking with there ip’s)

2.Check your current IP address by visiting whatismyip.com (Same)

Regards

Answer:

have you try using an API
ex:
http://ip-api.com/json

that will return country code and many other information all by analyze request IP “(no need obtained IP from device)” .

but obvious we need assume user not use any proxy or vpn

(There are plenty of other apis or you can implement and host one by yourself)

Answer:

No way of getting it without Location permission, your better approximation is the localization by ip but as you saw there is no guarantee of precise localization