-1

"I need to switch Base URL based on a user's Location and change other UI components".

I tried throughout the Build Config but I didn't find anything that seems to work.

Dut A.
  • 1,029
  • 11
  • 22
  • Some code snippet on what you've tried or what you are trying to do would do us good for faster help. – Dut A. Jul 09 '19 at 17:13

1 Answers1

0

First you have to add servers to strings.xml file then you can use this one to get location country name after that in switch case you can change the server

@Override
protected void onResume() {
    String country_name = null;
    LocationManager lm =
            (LocationManager)getApplicationContext().getSystemService(Context.LOCATION_SERVICE);
    Geocoder geocoder = new Geocoder(getApplicationContext());
    for(String provider: lm.getAllProviders()) {
        @SuppressWarnings("ResourceType") Location location = lm.getLastKnownLocation(provider);
        if(location!=null) {
            try {
                List<Address> addresses = geocoder.getFromLocation(location.getLatitude(), location.getLongitude(), 1);
                if(addresses != null && addresses.size() > 0) {
                    country_name = addresses.get(0).getCountryName();
                    break;
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
    Toast.makeText(getApplicationContext(), country_name, Toast.LENGTH_LONG).show();
}
AhmetAcikalin
  • 328
  • 2
  • 7