Android Xamarin 后台服务 - 每 x 分钟获取一次 GPS 位置? [英] Android Xamarin Background service - Get GPS Location every x minutes?

查看:57
本文介绍了Android Xamarin 后台服务 - 每 x 分钟获取一次 GPS 位置?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 Xamarin 开发一个应用程序,我需要一个后台服务来定期(例如每 15 分钟)获取用户的 GPS 位置.

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).

这个项目(https://github.com/xamarin/mobile-samples/tree/master/BackgroundLocationDemo) 是我能找到的最接近示例代码的代码,它声称位置会继续更新,即使应用程序在后台.开箱即用,情况似乎并非如此.

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.

这是应用程序进入后台后的日志,然后它停止更新 GPS.我只是想知道这是否是正常行为,因为项目描述另有说明.

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

一旦应用事件:停止"发生,应用就会进入后台,GPS 服务不再起作用.另外,我使用的是 Android 8.0 - 不确定这是否与它有关.

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?

推荐答案

感谢 @SushiHangover,我创建了一个前台服务.现在效果很好.

I created a Foreground Service, thanks to @SushiHangover. It works very well now.

这篇关于Android Xamarin 后台服务 - 每 x 分钟获取一次 GPS 位置?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆