0

Current Country can be obtained as follows:

String locale = context.getResources().getConfiguration().locale.getCountry(); 

But what about current city/town? Is there a way to get that? If not possible from the phone, then perhaps by calling an external service?

Jasper
  • 8,440
  • 31
  • 92
  • 133
  • possible duplicate of [What is the simplest and most robust way to get the user's current location in Android?](http://stackoverflow.com/questions/3145089/what-is-the-simplest-and-most-robust-way-to-get-the-users-current-location-in-a) – Flow Jun 02 '15 at 10:11

2 Answers2

0
    Add these lines in manifest :
        <manifest ... >
            <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
           <uses-permission android:name="android.permission. ACCESS_COARSE_LOCATION" />
           <uses-permission android:name="android.permission.INTERNET" />
        </manifest>

And in your Code

    LocationManager locationManager ;
        String provider;
    Geocoder geocoder;
    List<Address> addresses;

        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);

            // Getting LocationManager object
            locationManager = (LocationManager)getSystemService(Context.LOCATION_SERVICE);        

            // Creating an empty criteria object
            Criteria criteria = new Criteria();

            // Getting the name of the provider that meets the criteria
            provider = locationManager.getBestProvider(criteria, false);


            if(provider!=null && !provider.equals("")){

                // Get the location from the given provider 
                Location location = locationManager.getLastKnownLocation(provider);

                locationManager.requestLocationUpdates(provider, 20000, 1, this);


                if(location!=null)
                    onLocationChanged(location);
                else
                    Toast.makeText(getBaseContext(), "Location can't be retrieved", Toast.LENGTH_SHORT).show();

            }else{
                Toast.makeText(getBaseContext(), "No Provider Found", Toast.LENGTH_SHORT).show();
            }
        }

        @Override
        public boolean onCreateOptionsMenu(Menu menu) {
            getMenuInflater().inflate(R.menu.activity_main, menu);
            return true;
        }

        @Override
        public void onLocationChanged(Location location) {
            // Getting reference to TextView tv_longitude
            TextView tvLongitude = (TextView)findViewById(R.id.tv_longitude);

            // Getting reference to TextView tv_latitude
            TextView tvLatitude = (TextView)findViewById(R.id.tv_latitude);

            // Setting Current Longitude
            tvLongitude.setText("Longitude:" + location.getLongitude());

            // Setting Current Latitude
            tvLatitude.setText("Latitude:" + location.getLatitude() );

geocoder = new Geocoder(this, Locale.getDefault());
 addresses = geocoder.getFromLocation(MyLat, MyLong, 1);
 String cityName = addresses.get(0).getAddressLine(0);
 String stateName = addresses.get(0).getAddressLine(1);
 String countryName = addresses.get(0).getAddressLine(2);
        }

        @Override
        public void onProviderDisabled(String provider) {
            // TODO Auto-generated method stub      
        }

        @Override
        public void onProviderEnabled(String provider) {
            // TODO Auto-generated method stub      
        }

        @Override
        public void onStatusChanged(String provider, int status, Bundle extras) {
            // TODO Auto-generated method stub      
        }
0

See Questions/Answeres How to get current location in Android

It used the GPSTracker class that helps to get current latitude,longitude,current street,city,state and Country.

Following Code Helpful to you :

    LocationManager location = (LocationManager)  getActivity().getSystemService(getActivity().LOCATION_SERVICE);
            if (location.isProviderEnabled(LocationManager.GPS_PROVIDER)) {
                GPSTracker gpsTracker = new GPSTracker(getActivity());
                gpsTracker.canGetLocation();
                Lat = gpsTracker.getLatitude() + "";
                Lon = gpsTracker.getLongitude() + "";

                if (gpsTracker.canGetLocation()) {

                    String country = gpsTracker.getCountryName(getActivity());
                    String city = gpsTracker.getLocality(getActivity());
                    String postalCode = gpsTracker.getPostalCode(getActivity());
                    String addressLine = gpsTracker.getAddressLine(getActivity());

                    current_location = addressLine + ", " + city + ", " + postalCode + ", " + country + "";

                    Log.d("country", country);
                    Log.d("city", city);
                    Log.d("postalCode", postalCode);
                    Log.d("addressLine", addressLine);
                    Log.d("current_location", current_location);

                }

              } else {
                Toast.makeText(getActivity(), "Please enable GPS from setting...", Toast.LENGTH_LONG).show();
            }
Community
  • 1
  • 1
Joseph Mekwan
  • 1,082
  • 1
  • 15
  • 28
  • So this requires GPS to be enabled... what if GPS is not enabled.. what is the next best fall back for that? – Jasper Apr 28 '15 at 11:09