问题
I'm developing an app using Xamarin, and I need a background service that gets the user's GPS location at recurring intervals (say, every 15 minutes).
This project (https://github.com/xamarin/mobile-samples/tree/master/BackgroundLocationDemo) is the closest I can find to example code, and it claims that the location continues to update, even with the app in the background. Out of the box, this doesn't seem to be the case.
Here's the log once the application moves into the background, and then it stops updating GPS. I'd just like to know if this is normal behaviour or not, as the project description says otherwise.
12-12 08:19:12.324 D/MainActivity(10254): Foreground updating
12-12 08:19:12.345 D/LocationService(10254): Latitude is -44.40749468
12-12 08:19:12.345 D/LocationService(10254): Longitude is 171.25077323
12-12 08:19:12.345 D/LocationService(10254): Altitude is 4
12-12 08:19:12.346 D/LocationService(10254): Speed is 2.37
12-12 08:19:12.346 D/LocationService(10254): Accuracy is 39.664
12-12 08:19:12.346 D/LocationService(10254): Bearing is 350.9
12-12 08:19:12.348 D/MainActivity(10254): Location status changed, event raised
12-12 08:19:12.917 D/AppTracker(10254): App Event: stop
12-12 08:19:12.931 D/MainActivity(10254): OnPause: Location app is moving to background
As soon as "App Event: stop" happens, the app moves into the background and the GPS Service no longer functions. Also, I use Android 8.0 - not sure if that has anything to do with it or not.
Could anyone offer insight (or even better, example code/projects) to point me in the right direction?
回答1:
I created a Foreground Service, thanks to @SushiHangover. It works very well now.
来源:https://stackoverflow.com/questions/47760074/android-xamarin-background-service-get-gps-location-every-x-minutes