Android定时任务(相当于cronjob)

发布于 2024-10-21 02:18:56 字数 379 浏览 4 评论 0原文

上次(由不同的用户)提出这个问题时,答案是:

如果这是在正在运行的活动中,您可以使用 Timer/TimerTask 和 Handler,或者可以使用 postDelayed() 和 AsyncTask。

这里: Android 重复任务

我仍在学习如何对 android 进行编程。我已经了解了我所知道的技能,包括线程,并且我的代码遇到了许多问题。任何人都可以举一个如何使用的示例:time/timertask 和处​​理程序或 postDelayed() 和 AsyncTask。

The last time this question was asked (by a different user), the answer response was:

If this is in a running activity, you could use Timer/TimerTask and a Handler, or you could use postDelayed() and an AsyncTask.

Here: Android Repetitive Task

I am still learning how to program android. I have gone through the skills I do know including threads and had many issues with my code. Can anyone give an example of how to use: time/timertask and handler OR postDelayed() and AsyncTask.

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

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

发布评论

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

评论(3

说谎友 2024-10-28 02:18:56

对于类似 Cron 的任务,您必须使用 AlarmManager,这是一个系统服务,要在代码中使用它,您需要调用:

AlarmManager myAlarmManager = Context.getSystemService(Context.ALARM_SERVICE).

有关 AlarmManager 的完整文档 此处。

For Cron like tasks you have to use AlarmManager, this is a system service, for using it in your code you need to call:

AlarmManager myAlarmManager = Context.getSystemService(Context.ALARM_SERVICE).

Full docs about AlarmManager here.

无妨# 2024-10-28 02:18:56

最合适的方法是通过服务。我通过查看 Android 附带的电子邮件应用程序的源代码,了解了如何编写服务。

总体思路是重写 Service 类,并设置 < a href="http://developer.android.com/reference/android/app/AlarmManager.html" rel="noreferrer">警报来激活您的服务。与守护进程和 Windows 服务不同,Android 服务并不总是运行 - 它们会启动(通常在由警报激活时)、执行工作,然后关闭。在某些情况下,您可能需要获取部分唤醒锁以保持服务将持续运行直至完成任务 - 否则,Android 可能会过早终止您的服务。

The most suitable approach is through services. I learned how to write services by looking at the source code for the stock Email app that is included with Android.

The general idea is that you override the Service class, and set up alarms to activate your service. Unlike daemons and Windows services, Android services aren't always running - they start up (usually when activated by an alarm), perform work, then shut down. In some cases, you may need to acquire a partial wake lock to keep the service going until it completes the task - otherwise, Android may kill your service prematurely.

春庭雪 2024-10-28 02:18:56

如果你想构建一个 cronjob 运行程序,那么你想要的是一个 服务

服务是一个应用程序组件,可以在后台执行长时间运行的操作,并且不提供用户界面。另一个应用程序组件可以启动一个服务,即使用户切换到另一个应用程序,它也会继续在后台运行。此外,组件可以绑定到服务以与其交互,甚至执行进程间通信(IPC)。例如,服务可能会在后台处理网络事务、播放音乐、执行文件 I/O 或与内容提供商交互。

If you want to build a cronjob runner then what you want is a Service:

A Service is an application component that can perform long-running operations in the background and does not provide a user interface. Another application component can start a service and it will continue to run in the background even if the user switches to another application. Additionally, a component can bind to a service to interact with it and even perform interprocess communication (IPC). For example, a service might handle network transactions, play music, perform file I/O, or interact with a content provider, all from the background.

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