Android的后台服务和AlarmManager [英] Android background service and AlarmManager

查看:166
本文介绍了Android的后台服务和AlarmManager的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我编码的Andr​​oid应用程序获取用户的位置,每隔5分钟,将其存储在数据库中,并将其发送到服务器。

我看过这样做的许多方面,我要做到以下几点:

  1. 在用户启动应用程序
  2. 在主界面活性启动的服务。
  3. 的服务运行在后台并保持接通和断开的全球定位系统,以及建立新的 线程,将保存到数据库中,并且将数据发送到服务器。

不过,我已经看到,它可以与一个远程服务来实现(http://developer.android.com/resources/samples/ApiDemos/src/com/example/android/apis/app/RemoteService.html)或与AlarmManager的时间表启动该服务,每5分钟。

的服务将需要始终运行:每间隔(5分钟)后,它被执行是很重要的

我想我在这里需要一些透明度。

感谢你的帮助,

解决方案
  

我编码的Andr​​oid应用程序获取用户的位置,每隔5分钟,将其存储在数据库中,并将其发送到服务器。

请允许用户选择的位置提供者。不是每个人都有自己的GPS设备上。不是每个人都拥有GPS功能。而且,并不是每个人都需要GPS的力量击中被打开每五分钟。

请允许用户选择轮询周期,包括永不民意调查 - 我会从活动手动刷新信息。另外,请履行后台数据设置(pcated作为ICS的德$ P $)。

  

我想我在这里需要一些透明度。

如果轮询应该去,即使该活动是不是在前台,使用 AlarmManager 。然而,大多数的食谱,使用 AlarmManager 将有真正的工作(在你的情况下,GPS定位和网络I / O)被一个 IntentService <处理/ code>。这不会在你的情况下工作,因为GPS是异步的 - 你不能只是得到修复,只要你喜欢它的感觉。这将需要很长的时间,可能是永远,得到修复,所以你必须要处理的延迟,并最终超时操作。写服务做,这是可能但棘手,特别是如果你的目标是即使设备睡着了收集这些信息。

如果,但是,轮询只应该去上,而该活动是在前台和设备上,我也不会用服务烦的所有。只是有活动中使用 postDelayed()来建立一个每五分钟的code定位运行,然后把它做的工作。

I am coding an android application that gets the user position every 5 minutes, stores it in the database and sends it to a server.

I have read many ways of doing it, I was going to do the following:

  1. User starts the application
  2. The main UI activity starts a service.
  3. The service runs in background and keeps turning on and off the gps, and creating new threads that will save to database,and send the data to the server.

But I have seen that it can be done with a "Remote service" (http://developer.android.com/resources/samples/ApiDemos/src/com/example/android/apis/app/RemoteService.html) or with an AlarmManager that schedules starting this service every 5 minutes.

The service will need to be running always: it is important that after every interval (5 minutes), it is executed.

I think I need some clarity here.

Thank you for your help,

解决方案

I am coding an android application that gets the user position every 5 minutes, stores it in the database and sends it to a server.

Please allow the user to choose the location provider. Not everybody has GPS on their device. Not everybody has GPS enabled. And, not everybody will want the power hit of GPS being turned on every five minutes.

Please allow the user to choose the polling period, including "never poll -- I'll refresh the information manually from the activity". Also, please honor the background data setting (deprecated as of ICS).

I think I need some clarity here.

If the polling is supposed to go on even if the activity is not in the foreground, use AlarmManager. However, most recipes for using AlarmManager will have the real work (in your case, GPS fix and network I/O) be handled by an IntentService. This will not work in your case, because GPS is asynchronous -- you cannot just get a fix whenever you feel like it. It will take a long time, possibly forever, to get a fix, so you have to deal with the delay and eventually timing out the operation. Writing a Service to do this is possible, but tricky, particularly if you are aiming to collect this information even if the device falls asleep.

If, however, the polling is only supposed to go on while the activity is in the foreground and the device is on, I wouldn't bother with a Service at all. Just have the activity use postDelayed() to set up an every-five-minutes scheduled bit of code to run, then have it do the work.

这篇关于Android的后台服务和AlarmManager的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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