制作倒数计时器并在后台运行 [英] Making a countdown timer and run it in the background

查看:95
本文介绍了制作倒数计时器并在后台运行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在我的应用程序中添加一个静音按钮,当用户单击该按钮时,他选择静音的持续时间,并且在启用静音后,用户将不会从我的应用程序收到通知。

I want to add a mute button to my app that when the user clicks it, he chooses a duration of the mute and while the mute is enabled the user won't get notifications from my app.

我认为 NSTimer 是一个不错的选择,但是我该如何使用它呢?以及如何设置计时器也将在后台运行?

I thought NSTimer is a good option, but how can I make a timer with it? And how can I set that the timer will run in the background aswell?

注意:我正在使用位置

推荐答案

我认为您不需要在后台运行计时器-或任何复杂的解决方案:

I don't think you need a running timer in the background - or any complicated solution:


  • 当您的应用程序位于前台时,请使用计时器(通常)。在启动时取消静音模式。

  • Use a timer when your app is in the foreground (as usual). Cancel the mute mode when it fires.

当您的应用切换到后台模式且计时器处于活动状态时,请取消计时器或使其运行,具体取决于您的需求。

When your app switches to the background mode and the timer is active, cancel the timer or let it running, depending on your needs.

(可选)当您的应用在后台模式下执行时,只需不发送通知即可。

Optionally, when your app is executing in background mode, just don't send notifications.

如果您的应用不久之后被暂停,则您的代码将无法执行。

If your app becomes suspended shortly after, your code wouldn't execute anyway.

如果您的应用切换到前台时仍然需要遵守静音状态,请计算您在静音时存储的日期时间值的持续时间模式已激活,并且当前的日期时间已与您的超时时间进行比较。

If your app still needs to obey the mute state when it switches to the foreground, calculate the duration of the date-time value which you stored when the mute mode has been activated and the current date-time and compare this with your timeout.

这篇关于制作倒数计时器并在后台运行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆