如何安排一些code的执行在Android或:究竟是Android的守护线程?线程、code、Android

2023-09-13 23:50:28 作者:花落忆流年丶

我目前正在对Android操作系统的应用程序,需要从远程服务器获取数据时有发生。

i'm currently working on an app for the android os that requires to fetch data from a remote server from time to time.

因为这更新,应进行即使实际的前端应用程序没有运行,我实现了,开始在系统启动的远程服务。现在我需要安排一个计时器开始更新。

as this "update" should be carried out even when the actual frontend app is not running, i implemented a remote service that is started on system boot. now i need to schedule a timer to start the update.

是定时级是正确的这份工作?如果是:是什么开始作为一个后台由T(真)

is the "Timer"-class the right one for this job? and if "yes": what is the difference between a "normal" Timer() and one started as a "daemon" by Timer(true)?

http://developer.android.com/reference/java/util/ Timer.html 是不是非常有帮助这一点:(

http://developer.android.com/reference/java/util/Timer.html isn't very helpful with this :(

编辑:

确定 - 我看到有更多的方法来做到这一点比我的预期。澄清:

ok - i see there are much more methods to do this than i expected. to clarify:

我想在指定的时间以执行一些code。 在此定时器用于触发code 7天以后的执行。 (即每星期在一个给定的工作日和时间) 的code应该运行没有唤醒手机,则它处于睡眠(屏幕变暗)。 运行code时,没有任何活动应该开始。即,没有应用程序弹出到屏幕上。 在该执行应该从互​​联网上获取一些数据的code。如果在这个时候没有互联网连接可用时,计时器应设置为某事像30分钟,然后重试。 完成code执行后,定时器将在接下来的时间将在7天后进行设置。 定时器应在系统启动,如启动,如果我重新启动手机,定时器应确定执行code和安排计时器的下一个日期。这有没有任何用户交互的工作!

在沉睡,线程/服务/定时器/无论如何不应占用任何系统资源,如果可能的话... 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 sth like 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!

when "sleeping", the thread/service/timer/whatsoever should not consume any system resources if possible...

我需要的是pretty的多是UNIX的cronjob。

what i need is pretty much a simple unix cronjob.

我想任何人知道这里newsrob为Android?我想知道的是pretty的大致相同的newsrob-updateservice。

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

推荐答案

使用 AlarmManager 。这使您可以设置您的日程安排,然后退出你的组件。您的code不需要保留在内存中,并会被触发时,警报响起。

Use AlarmManager. This allows you to set your schedule, then exit your components. Your code does not need to remain in memory and will be triggered when the alarm sounds.

我实现上启动系统启动了一个远程服务

如何在VsCode中安装Flutter并运行Android Emulator

i implemented a remote service that is started on system boot

请不要那样做只是一个计划任务。使用 AlarmManager

Please don't do that just for a scheduled task. Use AlarmManager.

如果你想要做的工作,而手机是睡着了,你就需要使用 _WAKEUP 报警类型,也许使用像我的 WakefulIntentService 以保持设备清醒时的工作正在做。

If you want the work to be done while the phone is asleep, you will need to use a _WAKEUP alarm type and perhaps use something like my WakefulIntentService to keep the device awake while the work is being done.