gpt4 book ai didi

javascript - 当应用程序退出或设备重新启动时,如何在cordova中使用javascript继续计时器(setTimeout())

转载 作者:行者123 更新时间:2023-11-28 05:43:23 25 4
gpt4 key购买 nike

我正在开发一个 cordova 应用程序,该应用程序使用户能够安排稍后自动发送的消息。我已经能够使用 javascript setTimeout() 成功安排消息,但我的问题是当我关闭应用程序或重新启动设备时,消息不会传递(就像 setTimeout() 清除一样)。即使在应用程序关闭或重新启动设备后,我该怎么做才能保持计时器运行。我已经尝试过后台模式,但只有当应用程序在后台且未完全关闭时才有效...我希望即使在应用程序关闭或设备重新启动时也能够运行我的计时器...提前致谢

最佳答案

Cordova plugin以防止应用程序在后台进入休眠状态。

大多数移动操作系统都具有多任务处理能力,但大多数应用程序不需要在后台运行,也不需要向用户展示。因此,他们在后台模式下暂停应用程序,并在切换到前台模式之前恢复应用程序。系统在后台保持所有网络连接打开,但在应用恢复之前不会传送数据。

在应用程序中安装插件的命令

cordova plugin add https://github.com/katzer/cordova-plugin-background-mode.git

防止应用程序在后台休眠

为了防止应用程序在后台暂停,必须调用 backroundMode.enable 接口(interface)。

更多信息

The background mode will be activated once the app has entered the background and will be deactivated after the app has entered the foreground. To activate the background mode the app needs to be in foreground.

cordova.plugins.backgroundMode.enable();

关于javascript - 当应用程序退出或设备重新启动时,如何在cordova中使用javascript继续计时器(setTimeout()),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38730742/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com