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.
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();
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With