Why is my location deleted every 10 seconds, in androidstudio?

1

I am using android studio 3.0 and the emulator with android 8.0 and 6.0 to get the location using the gps of the device.

Problem: I get the location from the emulator, the corresponding onLocationChanged method is activated, but every 10 seconds this method is executed again without having changed the position in the emulator.

I imagine that my location is deleted and it takes the same position again as a new location every 10 seconds and therefore I have an infinite cycle running every 10 seconds

Is this behavior correct in this method?

public void GPS() {

    locationManager = (LocationManager) this.getSystemService(Context.LOCATION_SERVICE);
    locationListener = new LocationListener() {
        public void onLocationChanged(Location location) {

            latitud = String.valueOf(location.getLatitude());
            longitud = String.valueOf(location.getLongitude());
            System.out.println("................"+latitud+ " ; " +longitud);
        }

        public void onStatusChanged(String provider, int status, Bundle extras) {
        }

        public void onProviderEnabled(String provider) {
        }

        public void onProviderDisabled(String provider) {
        }
    };


    if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED ) {
         System.out.println("...........no hay permiso");
        return;
    }
        System.out.println(".............todo ok");
    locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, locationListener);
}
    
asked by Ironmario 27.11.2018 в 06:56
source

1 answer

1

Review the method requestLocationUpdates () from LocationManager

locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, MIN_TIEMPO_ENTRE_UPDATES, MIN_CAMBIO_DISTANCIA_PARA_UPDATES, locListener, Looper.getMainLooper());

in your case you have defined:

locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, locationListener);
  

minTime : minimum time interval between updates   location, in milliseconds

     

minDistance : minimum distance between location updates,   in meters

Therefore, possibly in the emulator try to get a position as fast as possible, even in production you must define appropriate values to avoid consumption of your battery.

I suggest you configure these values, for example that you ask for the new position every minute (60000 milliseconds) and when there is a change in the position of 5 meters.

 locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 60000, 5, locListener, Looper.getMainLooper());
    
answered by 27.11.2018 / 16:50
source