LocationListener destroy

0

Good afternoon forum friends, I am new to android, I am developing an application to save gps location on a server this is my code:

@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_registrar_cliente);
    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);

    Intent intent = getIntent();
    Bundle extras = intent.getExtras();

    final DecimalFormat format = new DecimalFormat();
    format.setMaximumFractionDigits(10);
    lugar = "";
    locationManager = (LocationManager) getSystemService(LOCATION_SERVICE);
    listener = new LocationListener() {
        @Override
        public void onLocationChanged(Location location) {
            //textView.append("n " + location.getLongitude() + " " + location.getLatitude());
            if (lugar == "D"){
                edLatitudD.setText("" +  format.format(location.getLatitude()));
                edLongitudD.setText("" + format.format(location.getLongitude()));
            }

            if (lugar == "N"){
                edLatitudN.setText("" +  format.format(location.getLatitude()));
                edLongitudN.setText("" + format.format(location.getLongitude()));
            }
        }

        @Override
        public void onStatusChanged(String s, int i, Bundle bundle) {

        }

        @Override
        public void onProviderEnabled(String s) {

        }

        @Override
        public void onProviderDisabled(String s) {

            Intent i = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
            startActivity(i);
        }




        public void onStop(){
            //super.onStop();  //<- CAMBIO
            locationManager.removeUpdates(listener);
        }
    };

}

@Override
 public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions,
                                       @NonNull int[] grantResults) {
 switch (requestCode) {
        case 10:
            configure_button();
            break;
        default:
            break;
    }
}

     // first check for permissions
    if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) !=
            PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this,
            Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            requestPermissions(new String[]{Manifest.permission.ACCESS_COARSE_LOCATION,
                            Manifest.permission.ACCESS_FINE_LOCATION, Manifest.permission.INTERNET}
                    , 10);
        }
        return;
    }

    locationManager.requestLocationUpdates("gps", 5000, 0, listener);


}

Everything works fine, but when I run locationManager.requestLocationUpdates ("gps", 5000, 0, listener); the cell location indicator always stays on until I close the application. How can I destroy this component or stop it when I do not use the application until I re-run the code, very grateful for the time and I hope you can help me.

    
asked by Marcelo 16.02.2018 в 22:37
source

0 answers