javascript 如何检查应用程序是在前台还是后台
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/25583861/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to check whether the app is in foreground or background
提问by vuimran
I am using phone gap to develop an android app. Is it possible to check if the app is running in background or foreground using javascript?
我正在使用手机间隙来开发一个 android 应用程序。是否可以使用 javascript 检查应用程序是在后台运行还是在前台运行?
As we can close the app by calling navigator.app.exitApp()
. We can perform other functions as well.
因为我们可以通过调用关闭应用程序navigator.app.exitApp()
。我们也可以执行其他功能。
Is there any function which can tell us whether the app is running in background or foreground?
有什么函数可以告诉我们应用程序是在后台运行还是在前台运行?
Actually, I want to make the app working in following way.
实际上,我想让应用程序按以下方式工作。
If app is in foreground, it should show an alert message rather than a push notification. If app is in background it should show a push notification.
如果应用程序在前台,它应该显示警报消息而不是推送通知。如果应用程序在后台,它应该显示推送通知。
Many thanks Indeed.
非常感谢确实。
回答by Saeid N.
Pause :
暂停 :
This is an event that fires when a Cordova application is put into the background.
这是在将 Cordova 应用程序置于后台时触发的事件。
document.addEventListener("pause", yourCallbackFunction, false);
Details
细节
Cordova consists of two code bases: native and JavaScript. While the native code puts the application into the background the pause event is fired.
Cordova 由两个代码库组成:本机和 JavaScript。当本机代码将应用程序置于后台时,会触发暂停事件。
Typically, you will want to attach an event listener with document.addEventListener once you receive the Cordova 'deviceready' event. Supported Platforms
通常,您会希望在收到 Cordova 'deviceready' 事件后使用 document.addEventListener 附加一个事件侦听器。支持的平台
- Android
- BlackBerry WebWorks (OS 5.0 and higher)
- iOS
- Windows Phone 7
- 安卓
- BlackBerry WebWorks(OS 5.0 及更高版本)
- IOS
- 视窗电话 7
Quick Example
快速示例
document.addEventListener("pause", onPause, false);
function onPause() {
// Handle the pause event
}
Resume :
恢复 :
This is an event that fires when a Cordova application is retrieved from the background.
这是从后台检索 Cordova 应用程序时触发的事件。
document.addEventListener("resume", yourCallbackFunction, false);
Details
细节
Cordova consists of two code bases: native and JavaScript. While the native code pulls the application from the background the resume event is fired.
Cordova 由两个代码库组成:本机和 JavaScript。当本机代码从后台拉取应用程序时,将触发恢复事件。
Typically, you will want to attach an event listener with document.addEventListener once you receive the Cordova 'deviceready' event. Supported Platforms
通常,您会希望在收到 Cordova 'deviceready' 事件后使用 document.addEventListener 附加一个事件侦听器。支持的平台
- Android
- BlackBerry WebWorks (OS 5.0 and higher)
- iOS
- Windows Phone 7
- 安卓
- BlackBerry WebWorks(OS 5.0 及更高版本)
- IOS
- 视窗电话 7
Quick Example
快速示例
document.addEventListener("resume", onResume, false);
function onResume() {
// Handle the resume event
}
More Information here :
更多信息在这里:
http://docs.phonegap.com/en/2.2.0/cordova_events_events.md.html#resume
http://docs.phonegap.com/en/2.2.0/cordova_events_events.md.html#resume
http://docs.phonegap.com/en/2.2.0/cordova_events_events.md.html#pause
http://docs.phonegap.com/en/2.2.0/cordova_events_events.md.html#pause
回答by Nathaniel D. Waggoner
It looks to me like you would register for a "pause" event - this would tell you that your application has gone to the background.
在我看来,你会注册一个“暂停”事件——这会告诉你你的应用程序已经进入后台。
http://docs.phonegap.com/en/1.1.0/phonegap_events_events.md.html#pause
http://docs.phonegap.com/en/1.1.0/phonegap_events_events.md.html#pause
You would then register for a resume event to receive notification of when you're in the foreground (although program logic should probably kick in at this point anyway).
然后,您将注册一个 resume 事件以接收有关您何时处于前台的通知(尽管此时程序逻辑可能应该启动)。
http://docs.phonegap.com/en/1.1.0/phonegap_events_events.md.html#resume
http://docs.phonegap.com/en/1.1.0/phonegap_events_events.md.html#resume
I'm not a phone gap user, but those seem like the most clear choice as i'm looking over the api.
我不是电话间隙用户,但当我查看 api 时,这些似乎是最明确的选择。