1

I want to get the current location details periodically and when there is considerable change from the previous location, it should update even when the app is not open. I don't see any samples to do this. Please update me how can it be done (correct me if I'm wrong)

user1799171
  • 567
  • 1
  • 3
  • 15

2 Answers2

1

You can set a minimum distance for receiving location updates using i.e. the following method: https://developer.android.com/reference/android/location/LocationManager.html#requestLocationUpdates(java.lang.String,%20long,%20float,%20android.location.LocationListener)

Also, you can listen to location updates using a Service that runs regardless whether the app is open or closed. You can make this service start up whenever the app opens and/or the device boots up. For more info on that look at this question: Trying to start a service on boot on Android

This way it is always running and will receive location updates as per your requirements.

Community
  • 1
  • 1
whitebrow
  • 2,015
  • 21
  • 24
  • Tried the way you mentioned. but it consumes a lot of battery power, within 4 hours it drains completely. Is there any alternative way to handle this? – user1799171 Sep 16 '16 at 10:06
-2

This is a nice android library`

LocationTracker tracker;
TrackerSettings settings =
                new TrackerSettings()
                        .setUseGPS(true)
                        .setUseNetwork(true)
                        .setUsePassive(true)
                        .setTimeBetweenUpdates(1000)
                        .setMetersBetweenUpdates(1);

        if (ActivityCompat.checkSelfPermission(this, android.Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, android.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;
        }
tracker = new LocationTracker(this, settings) {

            @Override
            public void onLocationFound(Location location) {
                // Do some stuff when a new location has been found.

                //mMap.clear();

                Toast.makeText(MapsActivity.this,"Location :"+location.describeContents()+location.getSpeed()+"\n"+location.getAltitude()+"\n"+location.getLatitude()+"\n"+location.getLongitude()+"\n"+location.getProvider()+"\n"+location.getAccuracy(), Toast.LENGTH_SHORT).show();

                updateLocation(location);
                //updateLocationInMap(location);
                setLocationInMapFromFireBase();

            }

            @Override
            public void onTimeout() {

                Toast.makeText(MapsActivity.this, "Time out", Toast.LENGTH_SHORT).show();
            }
        };
        tracker.startListening();` 

Its very easy and simple

Vivek Mishra
  • 5,669
  • 9
  • 46
  • 84
amit ghosh
  • 81
  • 7