在活动之间保持 locationListener 处于活动状态
我在 Locator.java 类中启动一个活动,该活动启动一个 locacionListener 服务。该活动将仓位写入数据库。我想知道当我移动到下一个屏幕(活动)时,位置是否会继续写入数据库,或者我需要返回 Locator.java 继续写入。当我最小化应用程序时也是如此。
我考虑过使用服务,但我想确定我需要它。
I start an activity in my class Locator.java which starts a locacionListener Service. That activity writes positions to a database. I wonder if when i move to next screens(activities), positions will keep writing to the database, or I would need to go back to Locator.java to keep writting. The same when I minimize the application.
I have thouhgt about using a Service, but I want to be sure I need it.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
只要服务启动,并且设备上有足够的可用资源,该服务就会继续在后台运行。切换活动、最小化或关闭应用程序不会改变这一点。由您决定是否正式停止服务。
但是,捕获位置更新并将其存储在数据库中的业务逻辑应该在服务中实现,而不是在活动中实现。 (该活动可用于控制服务的状态(停止-启动),并显示来自数据库的数据,但是数据库逻辑中的大部分位置更新/存储应该从服务触发。
您可以检查在您的 Android 设备上运行服务(设置 - 应用程序 - 运行服务)。您应该看到您的服务,以及它已运行的时间,
具体取决于您的要求,如果您也这样做,则简单的服务不会减少它。想要在设备睡眠时捕获位置更新并将其存储在数据库中,您需要查看 WakefullintentService
As long as the service is started, and providing sufficient resources are available on the device, the service will continue to run in the background. Switching activities, minimizing or closing the app doesn't change that. It's up to you to formally stop the service.
The business logic for capturing location updates and storing them in the database should be implemented in the service however, and not in the activity. (The activity can be used to control the state of the service (stop-start), and to display data from the database, but the bulk of your location update / storing in database logic should be triggered from the service.
You can check the running services on your android device (Settings - Applications - Running Services). You should see your service, and the amount of time it has been running.
Depending on your requirements, a simple service will not cut it. If you for example you also want to capture location updates and store them in a database while the device is sleeping, you'll need to look at WakefullintentService