How to check whether the app is in foreground or background
Pause :
This is an event that fires when a Cordova application is put into the background.
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.
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 and higher)
- iOS
- 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.
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.
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 and higher)
- iOS
- 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#pause
Comments
-
vuimran about 2 years
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?
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.
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.