jueves, 20 de febrero de 2014

Android: How to get the country the user is currently at

The following example grabs the position of the user  from either the GPS or other source

LocationManager locationManager = (LocationManager) activity.getSystemService(Context.LOCATION_SERVICE);

locationListener = new LocationListener() {

public void onLocationChanged(Location location) {

try {
Geocoder gcd = new Geocoder(activity, Locale.getDefault());
List<Address> addresses;

addresses =
gcd.getFromLocation(location.getLatitude(), location.getLongitude(), 1);

if (addresses.size() > 0) {

// String countryCode = addresses.get(0).getCountryCode();

// Do something with it

}
catch (IOException e) {

e.printStackTrace();
}
}

public void onStatusChanged(String provider, int status, Bundle extras) {

}

public void onProviderEnabled(String provider) {

}

public void onProviderDisabled(String provider) {

}
};

locationManager.requestLocationUpdates(
LocationManager.NETWORK_PROVIDER, 0, 0, locationListener);

locationManager.requestLocationUpdates(
LocationManager.GPS_PROVIDER, 0, 0, locationListener);

No hay comentarios:

Publicar un comentario