如何在 Windows 10 UWP javascript 中检查 foreground/background 状态

How to check foreground/background status in Windows 10 UWP javascript

我正在 javascript 中开发 Windows 10 UWP 应用程序。 其中一项要求是显示通知,但仅当应用程序处于后台而不是前台时。

我的问题是,使用 javascript 检查应用程序在 background/foreground 中是否为 运行 的最佳方法是什么。非常感谢。

what is the best way to check if the app is running in background/foreground, using javascript.

如果勾选app lifecycle, you could use WebUIApplication.EnteredBackground Event and WebUIApplication.LeavingBackground Event to do judgement. For example, you could use ApplicationData.LocalSettings来存储当前应用的状态。当触发 EnteredBackgroundLeavingBackground 事件时,您可以在其事件处理程序中更改此 localsettings 值。然后,当你想要检测应用程序的状态时,你可以获取这个localsettings值并进行判断。

您使用的 API 与您在网页中使用的 API 相同:在失去焦点和重新获得焦点时使用 document.hasFocus() to determine whether the app has focus. You will also receive the window.blur and window.focus 事件。