6

I am using Firebase auth with google sign in and facebook sign in

Is there any other way to know which country the user is coming from ?

After The app know from which country the user is coming from, then the app will decide which service / future will be shown to the user.

Plugie
  • 1,289
  • 17
  • 25
  • There are many way to get user country code. see this answer http://stackoverflow.com/questions/11293642/how-can-i-get-my-android-device-country-code-without-using-gps – Vikash Kumar Verma Dec 16 '16 at 06:55
  • +Vikash Kumar Verma. I need it based on google plus / facebook profile. Because, mobile phone configuration not always based on which country the user live. I use english in my mobile phone but not live in english. – Plugie Dec 17 '16 at 06:51
  • See my answer any let me know if it help – Vikash Kumar Verma Dec 17 '16 at 12:24

1 Answers1

12

Method 1: Try this code. This code will return country code based on which country's network it is connected to. see here. It won't work without SIM card.

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

Method 2: If your device is connected to internet you can use this link http://ip-api.com/json . It will automatically detect ip address of device return location details based on ip address.

Method 3: You can get location from GPS.

 LocationManager locationManager = (LocationManager)getSystemService(Context.LOCATION_SERVICE);
Criteria criteria = new Criteria();
criteria.setAccuracy(Criteria.ACCURACY_FINE);
String provider = locationManager.getBestProvider(criteria,true);
Location curLocation = locationManager.getLastKnownLocation(provider);
Geocoder geocoder = new Geocoder(context, Locale.getDefault());
List<Address> addresses = null;
    addresses = geocoder.getFromLocation((double)curLocal.getLatitudeE6(),(double)curLocal.getLongitudeE6(),1);
String countryCode= addresses.get(0).getCountryCode();
Vikash Kumar Verma
  • 1,068
  • 2
  • 14
  • 30
  • Good answer. I pick this as an answer of my question because it's solve my problem although the method is the alternative way instead of using firebase. I will choose the method 2 to be implemented in my app, because the app need internet connection. For method 1, not always user uses sim card. For method 3, it needs GPS permission. Thanks – Plugie Dec 21 '16 at 00:53
  • 1
    In many cases, ip-api.com will only detect the location of the user’s s proxy server. If they are using a VPN service then this will likely be outside their country. – Tony O'Hagan Jun 10 '18 at 14:24
  • @Plugie can you please guide me how to extract country using 2nd link method? – Ankit Mishra Sep 02 '20 at 18:32
  • @AnkitMishra Unity 2019: https://docs.unity3d.com/2019.4/Documentation/ScriptReference/Networking.UnityWebRequest.Get.html Unity 2020: https://docs.unity3d.com/2020.3/Documentation/ScriptReference/Networking.UnityWebRequest.Get.html – Tritmm Apr 25 '22 at 03:18
  • @Plugie you should be careful when using method 2. It's not free for commercial usages as mentioned in their website. – Ali Naderi Jan 06 '23 at 08:39