[英]How to check whether the app is in foreground or background
I am using phone gap to develop an android app. 我正在使用手机差距开发Android应用程序。 Is it possible to check if the app is running in background or foreground using javascript? 是否可以使用javascript检查应用程序是在后台运行还是在前台运行?
As we can close the app by calling navigator.app.exitApp()
. 我们可以通过调用navigator.app.exitApp()
来关闭应用程序。 We can perform other functions as well. 我们也可以执行其他功能。
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. 如果app在后台,则应显示推送通知。
Many thanks Indeed. 非常感谢。
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. Cordova包含两个代码库:本机代码和JavaScript代码。 While the native code puts the application into the background the pause event is fired. 当本机代码将应用程序置于后台时,会触发暂停事件。
Typically, you will want to attach an event listener with document.addEventListener once you receive the Cordova 'deviceready' event. 通常,一旦收到Cordova的“deviceready”事件,您将需要使用document.addEventListener附加事件侦听器。 Supported Platforms 支持的平台
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. Cordova包含两个代码库:本机代码和JavaScript代码。 While the native code pulls the application from the background the resume event is fired. 当本机代码从后台提取应用程序时,将触发resume事件。
Typically, you will want to attach an event listener with document.addEventListener once you receive the Cordova 'deviceready' event. 通常,一旦收到Cordova的“deviceready”事件,您将需要使用document.addEventListener附加事件侦听器。 Supported Platforms 支持的平台
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
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). 然后,您将注册一个简历事件,以接收有关您何时处于前台的通知(尽管程序逻辑应该在此时启动)。
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。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.