This question has already been answered:

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";//И ОТОБРАЖАЕТ ЭТУ НФОРМАЦИЮ НА ЭКРАНЕ } 

Reported as a duplicate by participants in Vladyslav Matviienko , user194374, aleksandr barakin , Denis , zRrr Jul 15 '16 at 11:06 .

A similar question was asked earlier and an answer has already been received. If the answers provided are not exhaustive, please ask a new question .

  • You have already asked this question. - Vladyslav Matviienko
  • @metalurgus is a new question on the same code. - Varg Sieg
  • @metalurgus in the last question you suggested to me how to change the value when the GPS is turned off this time, I’m wondering how to change the value when the GPS is in search of coordinates. - Varg Sieg

1 answer 1

 private void showLocation(Location location) { if (location == null) { dataGPS1 = "00,0000"; dataGPS2 = "00,0000"; return; } dataGPS1 = formatLocation1(location); dataGPS2 = formatLocation2(location); } 

If you want to decide not correctly, but quickly (with the help of a crutch), you can try this:
Before this line:

 file = new File(directory.getPath() + "/" + " " + dataGPS1 + " " + dataGPS2 + " " + ".jpg"); 

add this:

 if(dataGPS1 == null) { dataGPS1 = "00,0000"; } if(dataGPS2 == null) { dataGPS2 = "00,0000"; } 

In this case, you can remove the changes for the situation when the GPS is turned off, which you made after the previous question.

P.S. The code you have to say is a little strange . Very ugly and incomprehensible.

  • I don’t want to say anything bad, but this option didn’t work, it didn’t work when I offered it to me for the first time once again to clarify that for some reason dances with conditions and comparisons do not work. Maybe the code is not beautiful, but how can it not be clear when every method is commented on in it? - Varg Sieg
  • @VargSieg, can you then show the full code ? You do not show the piece where you cancel the GPS, use 'dataGPS1' - Vladyslav Matviienko
  • So this is the complete code of the entire block of methods responsible for GPS. The result of its work is stored in variables of type String GPS1 and GPS2 and then displayed in the file name file = new File(directory.getPath() + "/" + " " + dataGPS1 + " " + dataGPS2 + " " + ".jpg"); - Varg Sieg
  • It may be easier to ask such a question how to ensure that the given construct `return String.format ("% 2 $ .4f ", location.getLatitude (), location.getLongitude ());` to null returned 00.0000? - Varg Sieg
  • @VargSieg, it is important where you call this code. It is obvious that just at the time of calling the code in variables + dataGPS1 + "" + dataGPS2 nothing was recorded. You can check right before calling this code if there is something there or not. - Vladyslav Matviienko