简体   繁体   English

如何检查应用程序是在前台还是后台

[英]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 支持的平台

  • Android Android的
  • BlackBerry WebWorks (OS 5.0 and higher) BlackBerry WebWorks(OS 5.0及更高版本)
  • iOS iOS版
  • Windows Phone 7 Windows Phone 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. 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 支持的平台

  • Android Android的
  • BlackBerry WebWorks (OS 5.0 and higher) BlackBerry WebWorks(OS 5.0及更高版本)
  • iOS iOS版
  • Windows Phone 7 Windows Phone 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

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.

相关问题 如何在 ionic/cordova/phonegap 中检查在前台或后台运行的应用程序 - how to check app running in foreground or background in ionic/cordova/phonegap 如何检查应用程序现在是前台还是后台? - How to check if the application is foreground or background now? 如何检查移动网络浏览器是在移动设备的前台还是后台运行 - how to check mobile web browser is running in foreground or background on mobile device 如何在Windows 10 UWP JavaScript中检查前景/背景状态 - How to check foreground/background status in Windows 10 UWP javascript 如何在 Javascript Win8 Metro 应用程序中捕获前景和背景事件 - How to trap foreground and background events in a Javascript Win8 Metro App 如何检查节点js应用程序中的请求url参数是否为空? - How to check whether request url params are not null in node js app? 如何检查 state 是否没有空值在 React 应用程序中, - How to check whether the state has no empty value In React app, 如何使用jsp检查应用程序是否已安装在移动设备中? - How to check using jsp whether an app is installed in mobile or not? 检查 URL 是否已在后台脚本中打开 - Check whether an URL is already open or not in Background script 永远处于后台的节点应用程序,如何将其带到前台 - Node application with Forever in background, how to bring it to foreground
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM