如何实现更新服务在后台运行?

发布于 2024-09-27 02:47:41 字数 895 浏览 4 评论 0原文

我目前正在为 Android 操作系统开发一个显示一些数据的应用程序。为了确保此数据是最新的,应用程序需要不时从远程服务器获取更新。 由于数据变化不频繁,因此应每周更新一次。我想让用户能够选择更新的工作日和白天(并且可以选择完全禁用此功能)。

问题是:即使用户此时没有使用手机,即使手机当前处于睡眠状态,即使手机最近重新启动并且应用程序尚未启动,也应该执行此更新。

我首先想到的是一个远程服务,它在系统启动时启动,确定运行更新的时间,设置计时器,然后等待/睡眠计时器触发。

现在,我被告知我应该使用闹钟计时器或某种处理程序......我对这个主题的了解越多,似乎存在越多的方法来做到这一点。

现在,我有点迷失哪种方法最适合我......所以这就是我需要的:

  • 我想在指定的时间执行一些代码。
  • 该定时器用于在未来7天触发代码的执行。 (即每周在给定的工作日和时间)
  • 如果手机处于“睡眠”状态(屏幕变暗),则代码应该运行而不会唤醒手机。
  • 运行代码时,不应启动任何活动。即屏幕上没有弹出任何应用程序。
  • 执行的代码应该从互联网获取一些数据。如果此时没有可用的互联网连接,则应将计时器设置为休眠 30 分钟,然后重试。
  • 代码执行完成后,计时器将设置为下一个间隔,即 7 天后。
  • 计时器应在系统启动时启动,例如,如果我重新启动手机,计时器应确定执行代码的下一个日期并安排计时器。这必须在没有任何用户交互的情况下工作! (即应用程序本身没有启动)
  • 当“睡眠”时,线程/服务/计时器/任何东西不应该消耗任何系统资源(如果可能的话)...
  • 我需要的是一个简单的 unix cronjob。

    我想这里有人知道 Android 版的“newsrob”吗?我想要实现的与 newsrob-updateservice 几乎相同。

  • I'm currently working on an app for the Android OS that displays some data. To ensure this data is up-to-date, it is required that the app fetches updates from a remote server from time to time.
    As the data does not change very often, this update should be carried out once per week. I want to give the user the ability to choose the weekday and daytime of the update (and optionally disable this feature completely).

    The thing is: this update should be carried out even when the user is not using the phone at this moment, even when the phone is currently sleeping and even when the phone has been rebooted recently and the app hasn't been started yet.

    The first thing I thought of was a remote service that starts at system boot, determines the time when to run the update, sets a timer and then waits/sleeps for the timer to fire.

    Now, I was told I should rather use alarm timers or some kind of handlers... the more I read about this topic, the more ways to do this seem to exist.

    Now, I'm a bit lost which method is the best for me... so here is what I need:

  • I want to execute some code at a time that is specified.
  • This timer is used to trigger the execution of code 7 days in the future. (i.e., every week at a given weekday and time)
  • The code should run WITHOUT waking the phone up if it is "sleeping" (screen dimmed).
  • When running the code, no activity should be started. i.e. no app pops up on the screen.
  • The code that is executed should fetch some data from the internet. If at this time no internet connection is available, the timer should be set to sleep 30 minutes and then try again.
  • After completing the code execution, the timer will be set for the next interval, which will be 7 days later.
  • The timer should be started at system boot, e.g., if I reboot the phone, the timer should determine the next date to execute the code and schedule the timer. This has to work without ANY user interaction! (i.e. without the app being started itself)
  • When "sleeping", the thread/service/timer/whatsoever should not consume any system resources if possible...
  • What i need is pretty much a simple unix cronjob.

    I think anyone here knows "newsrob" for android? What I want to realize is pretty much the same as the newsrob-updateservice.

  • 如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

    扫码二维码加入Web技术交流群

    发布评论

    需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

    评论(1

    笑饮青盏花 2024-10-04 02:47:41

    Android 服务 + 广播接收器 + 警报服务将解决您的目的 -

    您的服务将从广播接收器调用,并且在广播接收器中您应该注册各种事件 - BOOT_RECEIVER 、 ACTION_USER_PRESENT ,它将处理您的警报重置和更新任务。

    谢谢。

    Android Service + Broadcast Receiver + Alarm Service will solve your purpose -

    Your service will be invoked from BroadCast Receiver and In Broadcast receiver you should register for various events - BOOT_RECEIVER , ACTION_USER_PRESENT , which will take care of your ALARM reset and update task.

    Thanks.

    ~没有更多了~
    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文