This question has already been answered:
- Default value for off GPS 2 answer
Good afternoon, there is GPS data which is reflected in the form of latitude 56.1922 and longitude 37.8615. The operation algorithm is such when the GPS receiver is ON and TO SEARCH FOR COORDINATES, the button is pressed to display the result which is obtained in the form of null null. It is necessary that this result be displayed in the form 00.0000 00.0000. Dancing with conditions and comparisons for some reason do not work.
protected void onResume() {// В onResume ВЕШАЕМ СЛУШАТЕЛЯ НА ПРОВАЙДЕРА С ПОМОЩЬЮ МЕТОДА requestLocationUpdates super.onResume(); if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) { // TODO: Consider calling // ActivityCompat#requestPermissions // here to request the missing permissions, and then overriding // public void onRequestPermissionsResult(int requestCode, String[] permissions, // int[] grantResults) // to handle the case where the user grants the permission. See the documentation // for ActivityCompat#requestPermissions for more details. return; } locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER/*ТИП ПРОВАЙДЕРА*/,// НА ВХОД ЕМУ ПОДАЁМ 1000 * 10/*МИНИМАЛЬНОЕ ВРЕМЯ ЗАПРОСА КООРДИНАТ*/, 10/*РАСТОЯНИЕ ОТОЙДЯ НА КОТОРОЕ ОБНОВЛЯЮТСЯ КООРДИНАТЫ*/, locationListener); } @Override protected void onPause() {//ОТКЛЮЧАЕМ СЛУШАТЕЛЯ МЕТОДА removeUpdates super.onPause(); if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) { // TODO: Consider calling // ActivityCompat#requestPermissions // here to request the missing permissions, and then overriding // public void onRequestPermissionsResult(int requestCode, String[] permissions, // int[] grantResults) // to handle the case where the user grants the permission. See the documentation // for ActivityCompat#requestPermissions for more details. return; } locationManager.removeUpdates(locationListener); } private LocationListener locationListener = new LocationListener() {//LocationListener СЛУШАТЕЛЬ РЕАЛИЗУЕТ ИНТЕРФЕЙС locationListener СО СЛЕДУЮЩИМИ МЕТОДАМИ @Override public void onLocationChanged(Location location) {//МЕТОД onLocationChanged НОВЫЕ ДАННЫЕ О МЕСТО ПОЛОЖЕНИИ showLocation(location); //ЗДЕСЬ ВЫЗЫВАЕМ СВОЙ МЕТОД showLocation(location)КОТОРЫЙ НА ЭКРАНЕ ОТОБРОЗИТ ДАННЫЕ О МЕСТО ПОЛОЖЕНИИ } @Override public void onStatusChanged(String provider, int status, Bundle extras) { } @Override public void onProviderDisabled(String provider) {//УКАЗАНЫЙ ПРОВАЙДЕР БЫЛ ОТКЛЮЧОН ПОЛЬЗОВАТЕЛЕМ checkDisabled1(); checkDisabled2(); } @Override public void onProviderEnabled(String provider) {//УКАЗАНЫЙ ПРОВАЙДЕР БЫЛ ВКЛЮЧОН ПОЛЬЗОВАТЕЛЕМ } }; private void showLocation(Location location) { if (location == null) return; dataGPS1 = formatLocation1(location); dataGPS2 = formatLocation2(location); } public String formatLocation1(Location location) {// НА ВХОД БЕРЁТ Location location if (location == null) //ЧЕТАЕТ ИЗ НЕГО ДАННЫЕ И ВЫДАЁТ СТРОКУ return ""; //ШИРОТА, ДОЛГОТА, ВРЕМЯ ОПРЕДЕЛЕНИЯ return String.format( "%1$.4f", location.getLatitude(), location.getLongitude()); } public String formatLocation2(Location location) {// НА ВХОД БЕРЁТ Location location if (location == null) //ЧЕТАЕТ ИЗ НЕГО ДАННЫЕ И ВЫДАЁТ СТРОКУ return ""; //ШИРОТА, ДОЛГОТА, ВРЕМЯ ОПРЕДЕЛЕНИЯ return String.format( "%2$.4f", location.getLatitude(), location.getLongitude()); } private void checkDisabled1() {//ОПРЕДЕЛЯЕТ ВКЛЮЧЕНЫ ИЛИ ВЫКЛЮЧЕНЫ ПРОВАЙДЕРЫ МЕТОДОМ isProviderEnabled dataGPS1 = "00,0000";//И ОТОБРАЖАЕТ ЭТУ НФОРМАЦИЮ НА ЭКРАНЕ } private void checkDisabled2() {//ОПРЕДЕЛЯЕТ ВКЛЮЧЕНЫ ИЛИ ВЫКЛЮЧЕНЫ ПРОВАЙДЕРЫ МЕТОДОМ isProviderEnabled dataGPS2 = "00,0000";//И ОТОБРАЖАЕТ ЭТУ НФОРМАЦИЮ НА ЭКРАНЕ }