1 - Вы создаете переменные для LocationManager и LocationListener в методе onCreate.
2 - Проверьте, есть ли разрешение, чтобы выполнить обновление местоположения и получить lastKnownLocation из locationManager, иначе вы запрашиваете разрешение
3 - Создайте onRequestPermissionResult в основном классе и проверьте, есть ли затем выполните обновления местоположения
4 - Создайте разделенный метод, который включает переменную геокодера и создайте список, чтобы поместить координаты из вашего местоположения, поэтому, чтобы быть в безопасности, вы проверяете, существует ли Список, и если каждая информация в этом списке существует (getThoroughfare ==> для Street Address), (getLocality ==> для City / State), (getPostalCode ==> для Zip), (getAdminArea ==> для полного адреса)
5 - Наконец, вы вызываете этот метод после проверки разрешения с (параметр lastKnownLocation ==> для отображения адреса при запуске приложения) и в onLocationChanged (параметр location ==> для отображения адреса при изменении местоположения)
Кодовая часть:
LocationManager locationManager;
LocationListener locationListener;
@SuppressLint("MissingPermission")
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
locationManager = (LocationManager) this.getSystemService(Context.LOCATION_SERVICE);
locationListener = new LocationListener() {
@Override
public void onLocationChanged(Location location) {
updateLocation(location);
}
@Override public void onStatusChanged(String provider, int status, Bundle extras) {
}
@Override
public void onProviderEnabled(String provider) {
}
@Override
public void onProviderDisabled(String provider) {
}
};
if (ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED){
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, locationListener);
Location lastKnownLocation = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
updateLocation(lastKnownLocation);
}else {
ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.ACCESS_FINE_LOCATION}, 1);
}
}
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED){
if (ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED){
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, locationListener);
}
}
}
public void updateLocation (Locati на месте) {
Geocoder geocoder = new Geocoder(getApplicationContext(), Locale.getDefault());
try {
List<Address> listAddresses = geocoder.getFromLocation(location.getLatitude(),location.getLongitude(),1);
String address = "Could not find location :(";
if (listAddresses != null && listAddresses.size() > 0) {
if (listAddresses.get(0).getThoroughfare() != null) {
address = listAddresses.get(0).getThoroughfare() + " ";
}
if (listAddresses.get(0).getLocality() != null) {
address += listAddresses.get(0).getLocality() + " ";
}
if (listAddresses.get(0).getPostalCode() != null) {
address += listAddresses.get(0).getPostalCode() + " ";
}
if (listAddresses.get(0).getAdminArea() != null) {
address += listAddresses.get(0).getAdminArea();
}
}
Log.i("Address",address);
} catch (Exception e) {
e.printStackTrace();
}
}
}
Вы можете проверить заголовки HTTP и сделать обоснованное предположение. Вы можете, например, попробовать http://network-tools.com , там есть инструмент для получения HTTP-заголовков.
(проголосовали за перенос вопроса на superuser.com, BTW)
Не существует определенного способа определить используемый язык сервера, просто просматривая веб-страницу.
Могут быть некоторые подсказки, такие как URL-адреса, заканчивающиеся на php, jsp или asp (x). Но, честно говоря, это может быть уловка.
Зачем вам такая вещь?