如何检查在ionic/cordova/phonegap中前台或后台运行的应用程序

use*_*398 43 javascript cordova ionic-framework ionic3 ionic4

有没有办法检查应用程序是否在ionic/cordova/phonegap的前台或后台运行,我需要在android和ios上使用它,非常感谢

Sit*_*hys 48

使用两个事件" Pause"和" Resume".您可以在Apache Cordova Events文档中找到所有事件.

事件 - 暂停:

  • 当本机平台将应用程序置于后台时,通常在用户切换到其他应用程序时触发暂停事件.

活动 - 简历

  • 当本机平台将应用程序从后台拉出时,将触发resume事件.

您可以在其代码中添加Eventlistener.这两个事件将是:

暂停 - 快速示例

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

function onPause() {
    // Handle the pause event
}
Run Code Online (Sandbox Code Playgroud)

或像这样的完整示例:

<!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>
Run Code Online (Sandbox Code Playgroud)

简历 - 快速示例

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

function onResume() {
    // Handle the resume event
}
Run Code Online (Sandbox Code Playgroud)

或像这样的完整示例

<!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>
Run Code Online (Sandbox Code Playgroud)

如果您需要进一步的帮助,请试试并告诉我们!


Ale*_*ilo 43

对于Ionic 2和Ionic 3,解决方案是:

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;
        });
       }
    });
}
Run Code Online (Sandbox Code Playgroud)


Geo*_*rge 12

基于Sithys的Ionic小型服务回答:

factory('BackgroundCheck', function($ionicPlatform){
    var service = {};
    var inBackground = false;

    $ionicPlatform.ready(function() {        
        document.addEventListener("resume", function(){inBackground = false;}, false);
        document.addEventListener("pause", function(){inBackground = true;}, false);
    });

    service.isActive = function(){
        return inBackground == false;
    }
    return service;    
})
Run Code Online (Sandbox Code Playgroud)


小智 5

使用的角度抽象 ionic.Platform

//The pause event fires when the native platform puts the application
// into the background, typically when the user switches to a different 
// application.
$ionicPlatform.on('pause', function () {
    // Handle event on pause
});
// The resume event fires when the native platform
//  pulls the application out from the background.
$ionicPlatform.on('resume', function () {
    // Handle event on resume
});
Run Code Online (Sandbox Code Playgroud)

有关$ ionicPlatform的信息,请参阅ionic v1文档。


tfm*_*gue 5

"有没有办法检查应用程序是在前台运行还是在后台运行?"

是.

1)当应用程序变为非活动状态(在后台运行)时,Cordova会触发pause事件,当应用程序再次变为活动状态(将其带到前台)时,Cordova会触发该resume事件.

2)从这些事件中,可以使用变量将状态存储为"前景"或"背景".

document.addEventListener("deviceReady", function readyCallback() {


    var isAppInForeground = true;


    document.addEventListener("pause", function pauseCallback() {
      isAppInForeground = false;
    }, false);

    document.addEventListener("resume", function resumeCallback() {
      isAppInForeground = true;
    }, false);

});
Run Code Online (Sandbox Code Playgroud)


Sam*_*ath 5

17/09/2019

这在Ionic 4应用程序上对我来说很好用。在AndroidiOS设备上都进行了测试。

应用程序组件.ts

async initializeApp() {
    await this.platform.ready();

    if (this.platform.is('cordova')) {
        this.setPlatformListener();
   }
 }


  setPlatformListener() {
    this.platform.pause.subscribe(() => {// background
      console.log('In Background');
    });

    this.platform.resume.subscribe(() => {// foreground
      console.log('In Foreground');
    });
  }
Run Code Online (Sandbox Code Playgroud)