Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to check whether the app is in foreground or background

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.

like image 917
vuimran Avatar asked Aug 30 '14 15:08

vuimran


People also ask

How do I know if an app is foreground or background?

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.

What is app foreground and background?

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.

What is app running in foreground?

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.


1 Answers

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

like image 137
Morteza Soleimani Avatar answered Oct 19 '22 23:10

Morteza Soleimani