如何检查应用程序运行在前台或后台在离子/ cordova /phonegap

wnvonmuf  于 2022-12-27  发布在  其他
关注(0)|答案(9)|浏览(222)

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

w80xi6nr

w80xi6nr1#

使用两个事件“Pause“和“Resume“。您将在Apache Cordova Events Documentation中找到所有事件。
事件-暂停:

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

事件-恢复

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

你可以在你的代码中添加一个事件监听器。对于这两个事件,它们将是:

暂停-快速示例

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>

试试看,让我知道,如果你需要进一步的帮助!

swvgeqrz

swvgeqrz2#

对于离子2和离子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;
        });
       }
    });
}
muk1a3rh

muk1a3rh3#

基于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;    
})
hc2pp10m

hc2pp10m4#

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

是的。
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);

});
6g8kf2rb

6g8kf2rb5#

二零一九年九月十七日

这对我来说在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');
    });
  }
to94eoyn

to94eoyn6#

使用ionic.Platform的Angular 抽象

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

qcuzuvrc

qcuzuvrc7#

您还可以用途:

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

r1zk6ea18#

对于使用电容器的用户:App plugin可用于预订appStateChange事件。
对于Capacitor v2,导入并使用带有以下代码的插件:

import { Plugins } from '@capacitor/core';

const { App } = Plugins;

App.addListener('appStateChange', (state) => {
  // state.isActive contains the active state
  console.log('App state changed. Is active?', state.isActive);
});

对于电容器v3+,安装@capacitor/app,执行电容器同步(npx cap sync),然后导入并使用带有以下代码的插件:

import { App } from '@capacitor/app';

App.addListener('appStateChange', ({ isActive }) => {
  console.log('App state changed. Is active?', isActive);
});

电容v4+提供两个额外的事件,称为pauseresume,可以订阅这两个事件来代替appStateChange事件:
一个二个一个一个
根据电容器文档,appStateChange事件取决于iOS设备上的UIApplication.willResignActiveNotificationUIApplication.didBecomeActiveNotification,而pause事件取决于UIApplication.didEnterBackgroundNotificationresume事件取决于iOS开发上的UIApplication.willEnterForegroundNotification
我不熟悉这些事件如何不同的细节,但它们之间似乎确实存在差异(例如see this question)。
在Android上,appStateChangepauseresume事件似乎都依赖于相同的原生事件:x一米十七氮一x和x一米十八氮一x。
如果您需要检查应用程序的当前状态,而不等待上述任何事件触发,则可以对Capacitor v2+使用getState方法。

x9ybnkn6

x9ybnkn69#

initializeApp() {
  //Subscribe on pause i.e. background or lock phone
       this.platform.pause.subscribe(() => {
          console.log('pause')
      });
  //Subscribe on pause i.e. background or unlock phone
      this.platform.resume.subscribe(() => {
          console.log('resume');
     });
}

相关问题