GPS on, but no longer works in APP

Asked

Viewed 84 times

0

td well? Next, I’m implementing a GPS code in my APP. It was working everything normal until then, until the time I tested turn off the GPS of the phone - to see if it gave error in the same APP. Only when I activated the phone’s GPS again, it’s like my app no longer recognizes the GPS.

For all intents and purposes, follow my codes:

filing cabinet Mainactivity.java:

public class MainActivity extends AppCompatActivity {

    private TextView txtLatitude, txtLongitude;
    private String latitude, longitude;

    @Override
    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        txtLatitude = (TextView) findViewById(R.id.txtLat);
        txtLongitude = (TextView) findViewById(R.id.txtLong);

        ActivityCompat.requestPermissions(MainActivity.this, new String[]{Manifest.permission.ACCESS_FINE_LOCATION}, 124);

        GPSTracker g = new GPSTracker(getApplicationContext());
        Location l = g.getLocation();

        if(l != null){

            double lat = l.getLatitude();
            double lon = l.getLongitude();

            if(latitude == null && longitude == null) {

                latitude = String.format("%.8f", lat) + "\n";
                longitude = String.format("%.8f", lon) + "\n";

            }else{

                latitude = latitude + String.format("%.8f", lat) + "\n";
                longitude = longitude + String.format("%.8f", lon) + "\n";

            }

            txtLatitude.setText(latitude);
            txtLongitude.setText(longitude);

        }else{

            Toast.makeText(getApplicationContext(), "Erro na captura!", Toast.LENGTH_SHORT).show();

        }

    }

}

Remembering that in my Androidmanifest.xml I have the set permission:

<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>

I’m counting on someone’s help, please.

Thank you!!! :-)

  • Makes the mistake that’s in Toast ? Picks values that are incorrect ? What appears in Logcat ?

  • Sorry people!!! I went to test my code outside my workplace, and it worked perfectly. I went to take a deeper look at GPS codes and I realized that the point he captures is not always very precise, sometimes it changes a few meters each time I order to capture the result ... I spoke nonsense or proceeds? :-)

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.