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.
It's very easy to detect when an Activity goes background/foreground just by listening to the lifecycle events, onStop() and onStart() of that Activity.
Foreground refers to the active apps which consume data and are currently running on the mobile. Background refers to the data used when the app is doing some activity in the background, which is not active right now.
Running in the Foreground means your app is currently Fully Visible on your device, you can see it and interact with it and it will respond to you right away.
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
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
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With