1

Using the google location.getlatitiude and location.getlongitude by

location = locationManager.getLastKnownLocation(locationProvider);
Latitude = location.getLatitude();
Longitude = location.getLongitude();

This is done inside a recurring runnable that fires every 5 seconds. So far so good.

When I "pause" the app (click the back button so app is running in the background) the above calls do not get new locations (they keep returning the last longitude and latitude before the pause).

I know the runnable is still firing as I also write to a web database during the runnable and the database is being hit and updated (just with stalled latitude and longitude values).

Once the app is unpaused/resumed and becomes visible the longitude and latitude readings resume and update correctly.

location, locationManager and locationProvider are globals outside the Runnable and created and verified prior to setting the runnable running, so it isn;t an issue with creating them when the app is paused.

Any ideas as to why pausing causes this? I have tested on 2 different phones. Both the same result. Is there some way I can tell the location or locationmanager to keep working even when app is paused?

Phantômaxx
  • 37,901
  • 21
  • 84
  • 115
Some1Else
  • 715
  • 11
  • 26
  • Did you try with LocationListener class? – Yash Nov 03 '17 at 08:46
  • Have you test on device or emulator ? if device please specify OS as well. – Haresh Chhelana Nov 03 '17 at 08:47
  • Devices. Galaxy Note 5 and Galaxy Note 8 at this stage. – Some1Else Nov 03 '17 at 08:52
  • LocationListener like the code [link]https://stackoverflow.com/questions/42218419/how-do-i-implement-the-locationlistener ? The internal code for getting the actual latitude and longitude is identical to my code, so I don;t think a listener will help? My issue is not the location code firing, but it not returning correct values. I will try setting up a locationlistener now to check and see if it does help. – Some1Else Nov 03 '17 at 08:54
  • LocationListener didn't seem to ever fire. I even tried creating a background service using this code [link]https://github.com/codepath/android_guides/issues/220 but that also stops updating once the app is paused. Very strange. Services are supposed to run when the app is in any state. – Some1Else Nov 03 '17 at 10:12

2 Answers2

2

Did you take a look to FusedLocationProviderApi?

You have two method to control the updates as you prefer:

LocationServices.FusedLocationApi.requestLocationUpdates(
            mGoogleApiClient, mLocationRequest, this);

and

LocationServices.FusedLocationApi.removeLocationUpdates(
                            mGoogleApiClient,this);

When the app is in background, if you don't call removeLocationUpdates you still get updates about your location. Take a look at this page of the documentation:

https://developer.android.com/training/location/receive-location-updates.html

Nicola Gallazzi
  • 7,897
  • 6
  • 45
  • 64
1

FusedLocationProvider was the hint I needed.

mLocationRequest = new LocationRequest();
mLocationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
mLocationRequest.setInterval(5000);
mLocationRequest.setFastestInterval(5000);
mLocationRequest.setSmallestDisplacement(10);
getFusedLocationProviderClient(getApplicationContext()).requestLocationUpdates(mLocationRequest, new LocationCallback() {
        @Override
        public void onLocationResult(LocationResult locationResult) {
           Toast.makeText(getApplicationContext(),locationResult.getLastLocation().getLatitude()+" "+locationResult.getLastLocation().getLongitude()+" "+locationResult.getLastLocation().getAccuracy(), Toast.LENGTH_SHORT).show();
        }
    },
    Looper.myLooper());

That code can be put inside the main activity oncreate. It fires every 5 seconds even when the app is paused. Previously I was using services etc to try and get this working. The new FusedLocationProvider is so much simpler (once you find working code). I hope the above helps somone else.

Some1Else
  • 715
  • 11
  • 26