如何检查应用程序是否在前台或后台 [英] How to check whether the app is in foreground or background

查看:1088
本文介绍了如何检查应用程序是否在前台或后台的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用手机差距开发一个Android应用程序。使用javascript可以检查应用程序是否在后台或前台运行?

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?

我们可以通过调用 navigator.app关闭应用程序。 exitApp()。我们可以执行其他功能。

As we can close the app by calling 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.

如果应用程序在前台,它应该显示警告消息,而不是推送通知。
如果app在后台,它应该显示推送通知。

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.

非常感谢。

推荐答案

暂停:

这是一个将Cordova应用程序放入后台时触发的事件。

This is an event that fires when a Cordova application is put into the background.

document.addEventListener("pause", yourCallbackFunction, false);

详细信息

Cordova由两个代码bases:native和JavaScript。虽然本机代码会将应用程序置于后台,但暂停事件会触发。

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后附加一个事件侦听器document.addEventListener deviceready事件。
支持的平台

Typically, you will want to attach an event listener with document.addEventListener once you receive the Cordova 'deviceready' event. Supported Platforms


  • Android

  • BlackBerry WebWorks(OS 5.0及更高版本) li>
  • iOS

  • Windows Phone 7

/ p>

Quick Example

document.addEventListener("pause", onPause, false);

function onPause() {
    // Handle the pause event
}

简历:

这是当从后台检索Cordova应用程序时触发的事件。

This is an event that fires when a Cordova application is retrieved from the background.

document.addEventListener("resume", yourCallbackFunction, false);

详细信息

Cordova由两个代码bases:native和JavaScript。虽然本机代码从后台拉取应用程序,但恢复事件被触发。

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后,您将需要使用document.addEventListener附加一个事件监听器deviceready事件。
支持的平台

Typically, you will want to attach an event listener with document.addEventListener once you receive the Cordova 'deviceready' event. Supported Platforms


  • Android

  • BlackBerry WebWorks(OS 5.0及更高版本) li>
  • iOS

  • Windows Phone 7

/ p>

Quick Example

document.addEventListener("resume", onResume, false);

function onResume() {
    // Handle the resume event
}

此处有更多信息:

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#暂停

这篇关于如何检查应用程序是否在前台或后台的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆