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

问题描述 投票:38回答:6

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

javascript cordova ionic
6个回答
46
投票

使用两个事件“Pause”和“Resume”。您将在Apache Cordova Events Documentation找到所有活动。

事件 - 暂停:

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

活动 - 简历

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

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

暂停 - 快速示例

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

function onPause() {
    // Handle the pause event
}

或像这样的完整示例:

<!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>

简历 - 快速示例

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

function onResume() {
    // Handle the resume event
}

或像这样的完整示例

<!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>

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


38
投票

对于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
        this.platform.pause.subscribe(() => {
          //Hello pause
        });

        //Subscribe on resume
        this.platform.resume.subscribe(() => {
          window['paused'] = 0;
        });
       }
    });
}

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;    
})

5
投票

"Is there any way to check whether the app is running in foreground or background?"

是。

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);

});

4
投票

使用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
});

请参阅ionic v1 documentation for $ionicPlatform


2
投票

您还可以使用:

import { Platform, Config, MenuController } from '@ionic/angular';

...

constructor( private platform: Platform)

...

this.platform.resume.subscribe(() => {
      // Handle event on resume
    });


this.platform.pause.subscribe(() => {
          // Handle event on pause
        });
© www.soinside.com 2019 - 2024. All rights reserved.