Android - 定时 UI 事件(带有暂停/恢复)

发布于 2024-09-07 20:25:05 字数 570 浏览 5 评论 0原文

我正在寻找创建触发 Android 上的 UI 更改(如 Toast)的定时事件。我已经知道您可以使用 Handler 对象的 postDelayed(runnable, timeDelay) 方法(请参阅此页面以获得一个很好的示例)。

这里有一个转折点:我还需要能够暂停和恢复这些事件的倒计时。因此,当用户暂停屏幕计时器时,它也会在每个事件触发之前暂停倒计时。

我想到的一种方法是在新的 Thread(不是 UI 线程)上创建一个 Handler 对象,并使用 postDelayed 对我的事件进行排队;然后我将使用

我能想到的唯一其他方法是每秒检查计时器是否针对 UI 线程上的所有事件列表(强力)进行检查,这非常混乱。

如果有人对此有任何想法或解决方案,那就太好了!

I am looking to create timed events which trigger UI changes (like a Toast) on Android. I already know you can do this with a Handler object's postDelayed(runnable, timeDelay) method (see this page for a great example).

Here is the twist: I also need to be able to pause and resume the countdown for these events. So when a user pauses the on-screen timer it also pauses the countdown before each of these events trigger.

One approach that came to mind was to create a Handler object on a new Thread (not the UI thread) and queue up my events using postDelayed; then I would pause or resume this entire Thread's execution using this method as needed. The main problem here is figuring out how to make the events run on the UI thread after they have been triggered.

The only other way I can think of would be to check every second that the timer ticks against a list of all my events (brute force) on the UI thread, which is very messy.

If anyone has any thoughts on this or solutions, that would be great!

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

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

发布评论

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

评论(3

朦胧时间 2024-09-14 20:25:05

如果所有事件需要暂停相同的时间,您可以跟踪累积的暂停时间,并在handleMessage上使用具有该延迟时间的另一个postDelayed重新发布。

需要添加一些逻辑以确保将它们延迟到适当的程度。例如,如果在游戏暂停时将新事件添加到队列中,您不希望延迟整个暂停时间来重新发布该事件。

您可以结合您的想法(具有所需开始时间的事件队列)。暂停结束时更新开始时间。使用单个事件“processQueue” - 将其设置为仅在您认为下一个事件应该发生时触发。如果下一个事件发生的时间由于暂停而延迟,您可以删除“processQueue”事件并重新发布它。

If all events need to be paused the same amount of time, you could keep track of how much pause time accumulated and, on the handleMessage, re-post using another postDelayed with that delay time.

Some logic would need to be added to make sure you delay them all the proper amount. For example, if a new event was added into the queue while the game was paused, you wouldn't want to re-post it delayed by the entire pause amount.

You could combine your idea (a queue of events with desired start times). Update that start time when the pause ends. Use a single event that is "processQueue" - set it to only fire when you think the next event should happen. If the time the next event should happen is delayed due to a pause, you can remove the "processQueue" event and re-post it.

乖乖哒 2024-09-14 20:25:05
  1. 为了能够在您自己的线程中使用处理程序,您需要有一个循环程序,请参阅 this 示例为了
  2. 能够在 UI 线程中运行命令,您可以在自己的线程代码中传递 Handler 引用,以便能够向其发布消息。
    希望有帮助
  1. To be able use a handler with your own thread you need to have a looper see this for an example
  2. To be able to run a command in UI Thread you can pass a Handler reference inside your own thread code to be able to post messages to it.
    Hope it helps
笔落惊风雨 2024-09-14 20:25:05

Handler#removeCallbacks 将让您取消安排已发布的 Runnable。当您知道它应该运行的新时间时,您可以稍后再次发布它。

Handler#removeCallbacks will let you un-schedule a posted Runnable. You can post it again later when you know the new time at which it should run.

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