Android 应用程序进入后台时未触发离子事件(平台暂停)

Android 应用程序进入后台时未触发离子事件(平台暂停),android,angular,cordova,ionic-framework,capacitor,Android,Angular,Cordova,Ionic Framework,Capacitor,我想在我的应用程序进入后台时执行一些操作,例如按下home(主页)按钮。(我正在安卓设备上测试。) 我在我的app.component.ts中尝试了以下操作: this.platform.ready().then(() => { this.platform.pause.subscribe(async () => { alert("Pause event detected"); //Do stuff here }); this.pla

我想在我的应用程序进入后台时执行一些操作,例如按下home(主页)按钮。(我正在安卓设备上测试。)

我在我的
app.component.ts中尝试了以下操作:

this.platform.ready().then(() => {
    this.platform.pause.subscribe(async () => {
      alert("Pause event detected"); 
      //Do stuff here
    });

    this.platform.resume.subscribe(async () => {
      alert("Resume event detected");
      //Do stuff here
    });
…
我还尝试:

App.getState().then((result) => {
  alert("state active?" + result.isActive);
});
当应用程序转到后台时(例如,按下home按钮),不会触发任何侦听器。但是,当我再次启动应用程序时,会触发所有事件(在本例中为警报),包括
platform.pause
事件

我用的是离子9和电容器


我是不是误解了什么?可能有什么问题?

您可以使用中提供的事件侦听器

然后在AppComponent类中

this.platform.ready().then(() => {

    App.addListener('appStateChange', (state: AppState) => {
        if (state.isActive) {
            console.log('App has become active');
        } else {
            console.log('App has become inactive');
        }
    });

})

请注意,您也可以通过切换到另一个选项卡在桌面浏览器中对此进行测试。

确定。。。事情是这样的。问题是,我在代码中激活了这两个变体

你是说9号和5号?对不起。。。在这些事件中有一个警告,不要使用诸如警报之类的阻止功能。你确定那不是问题吗?真的吗?这似乎不应该是一个问题。不幸的是,这是一个问题。我让他们都在同一个页面上工作,就像在Android中找到的一样,但只有AppState one在iOs上工作。
this.platform.ready().then(() => {

    App.addListener('appStateChange', (state: AppState) => {
        if (state.isActive) {
            console.log('App has become active');
        } else {
            console.log('App has become inactive');
        }
    });

})