Android的后台服务和AlarmManager

编程入门 行业动态 更新时间:2024-10-24 11:25:04
本文介绍了Android的后台服务和AlarmManager的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

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

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

  • 在用户启动应用程序
  • 在主界面活性启动的服务。
  • 的服务运行在后台并保持接通和断开的全球定位系统,以及建立新的 线程,将保存到数据库中,并且将数据发送到服务器。
  • 不过,我已经看到,它可以与一个远程服务来实现(developer.android/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 。这不会在你的情况下工作,因为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:

  • User starts the application
  • The main UI activity starts a service.
  • 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" (developer.android/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

    本文发布于:2023-11-27 09:28:54,感谢您对本站的认可!
    本文链接:https://www.elefans.com/category/jswz/34/1637513.html
    版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
    本文标签:后台   Android   AlarmManager

    发布评论

    评论列表 (有 0 条评论)
    草根站长

    >www.elefans.com

    编程频道|电子爱好者 - 技术资讯及电子产品介绍!