Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to check app running in foreground or background in ionic/cordova/phonegap

Is there any way to check whether the app is running in foreground or background in ionic/cordova/phonegap, I need to use it on android and ios, thanks a lot

like image 643
user1521398 Avatar asked Apr 13 '15 13:04

user1521398


People also ask

What is the difference between Phonegap Cordova and ionic?

Ionic, Cordova and PhoneGap are hybrid mobile apps. Cordova is a framework which runs a JavaScript app in a WebView that has additional native extensions, which is the definition of a hybrid app. Ionic is based on Cordova and comes with Angular or ReactJS. It has set of standard controls that mimic native controls.

What does running in foreground mean?

An app is considered to be in the foreground if any of the following is true: It has a visible activity, whether the activity is started or paused. It has a foreground service. Another foreground app is connected to the app, either by binding to one of its services or by making use of one of its content providers.

What is foreground in UI?

Overview. Use foreground transitions to prepare your app's UI to appear onscreen. An app's transition to the foreground is usually in response to a user action. For example, when the user taps the app's icon, the system launches the app and brings it to the foreground.


2 Answers

Use the two Events "Pause" and "Resume". You will find all Events here in the Apache Cordova Events Documentation.

Event - Pause:

  • The pause event fires when the native platform puts the application into the background, typically when the user switches to a different application.

Event - Resume

  • The resume event fires when the native platform pulls the application out from the background.

You can add an Eventlistener for that into your code. For those two Events that would be:

Pause - Quick Example

document.addEventListener("pause", onPause, false);  function onPause() {     // Handle the pause event } 

Or Full Example like this:

<!DOCTYPE html> <html>   <head>     <title>Pause Example</title>      <script type="text/javascript" charset="utf-8" src="cordova.js"></script>     <script type="text/javascript" charset="utf-8">      // Wait for device API libraries to load     //     function onLoad() {         document.addEventListener("deviceready", onDeviceReady, false);     }      // device APIs are available     //     function onDeviceReady() {         document.addEventListener("pause", onPause, false);     }      // Handle the pause event     //     function onPause() {     }      </script>   </head>   <body onload="onLoad()">   </body> </html> 

Resume - Quick Example

document.addEventListener("resume", onResume, false);  function onResume() {     // Handle the resume event } 

Or Full Example like this

<!DOCTYPE html> <html>   <head>     <title>Resume Example</title>      <script type="text/javascript" charset="utf-8" src="cordova.js"></script>     <script type="text/javascript" charset="utf-8">      // Wait for device API libraries to load     //     function onLoad() {         document.addEventListener("deviceready", onDeviceReady, false);     }      // device APIs are available     //     function onDeviceReady() {         document.addEventListener("resume", onResume, false);     }      // Handle the resume event     //     function onResume() {     }      </script>   </head>   <body onload="onLoad()">   </body> </html> 

Try that out and let me know, if you need further help!

like image 171
Sithys Avatar answered Oct 07 '22 21:10

Sithys


For Ionic 2 and Ionic 3 the solution is:

import { Platform } from 'ionic-angular';    @Component({    template: `OK`  })    constructor(public platform: Platform) {        platform.ready().then(() => {          if (platform.is('cordova')){            //Subscribe on pause i.e. background          this.platform.pause.subscribe(() => {            //Hello pause          });            //Subscribe on resume i.e. foreground           this.platform.resume.subscribe(() => {            window['paused'] = 0;          });         }      });  }
like image 22
Alexander Zakusilo Avatar answered Oct 07 '22 21:10

Alexander Zakusilo