What is the best way to get country code? As of now I know two ways one is to get by TelephonyManager and another by Locale which is the other best & unique way to find country code in android.
相关问题
- How can I create this custom Bottom Navigation on
- Bottom Navigation View gets Shrink Down
- How to make that the snackbar action button be sho
- Listening to outgoing sms not working android
- How to create Circular view on android wear?
相关文章
- android开发 怎么把图片放入drawable的文件夹下
- android上如何获取/storage/emulated/下的文件列表
- androidStudio有个箭头不认识
- SQLite不能创建表
- Windows - Android SDK manager not listing any plat
- Animate Recycler View grid when number of columns
- Why is the app closing suddenly without showing an
- Android OverlayItem.setMarker(): Change the marker
I know it's a very old question but I think this will help some of devs:
It's very hard to find the country without GPS and Google Services (China).
TelephonyManager
won't work if there is no SIM in your phone.And
Locale
won't work if a user in China set his language as English (the country you will be getting will be US or UK).If your app requires internet connection then there is an option. You could use this API called ip-api. Please go through there documentation first if you are planing to use this
There are other APIs like this freegeoip api
There are always huge discussions about this, and I never understand why developers and companies go for the complex way. The language selected by the user, means the language he/she wants to see always in his/her phone. They don't intend to have apps in a different language than others or the system.
The choice is very straight forward: Use Locale (to get the language the user selected as preferred), or make your best to piss them off showing them information in a language they already said they don't want to see things in.
To get the country code use:
I think ip is the best way because it will give you the country where the phone is at the momment,
If you do it by
you got the country where the user select the country so you can have selected England, and you can be in Spain and maybe you need to know where is him at the momment
Example: imagine that your app can buy something ONLY in england, the user is from Spain but he is on holidays on England and he want to buy your product ... if you use
that user wont be able to buy your product,so the ip is the best way i think
I solved it by IP Address. You can get IP Address of your phone. If you are using wifi then it will IP address of wifi hotspot or IP of your Mobile Service Provide server so from that IP you can send to web service or something to track country of that IP Address there are some sources(database) available if internet which provides country of IP here is the example http://whatismyipaddress.com/ip-lookup.
You can use this code to get ISO2:
Locale.getDefault().getCountry()
And this to get ISO3:
Locale.getDefault().getISO3Country()
Hope this helps
Try this,