离子背景模式 moveToForeground() 不起作用

时间:2021-02-24 04:47:58

标签: android angular ionic-framework capacitor background-mode

我正在尝试使用 Ionic 和后台模式插件构建一个简单的功能,当应用程序在 N 秒后发送到后台以将应用程序带回前台时。

npm i cordova-plugin-background-mode
npm install @ionic-native/background-mode

app.modules.ts

import { IonicModule, IonicRouteStrategy } from '@ionic/angular';

import { AppComponent } from './app.component';
import { AppRoutingModule } from './app-routing.module';

import { BackgroundMode } from '@ionic-native/background-mode/ngx';

@NgModule({
  declarations: [AppComponent],
  entryComponents: [],
  imports: [BrowserModule, IonicModule.forRoot(), AppRoutingModule],
  providers: [BackgroundMode, { provide: RouteReuseStrategy, useClass: IonicRouteStrategy }],
  bootstrap: [AppComponent],
})
export class AppModule {}

app.component.ts

import { Component } from '@angular/core';
import { BackgroundMode } from '@ionic-native/background-mode/ngx';

@Component({
  selector: 'app-root',
  templateUrl: 'app.component.html',
  styleUrls: ['app.component.scss'],
})
export class AppComponent {
  constructor(private backgroundMode: BackgroundMode) {
    console.log("application component started");
    let self = this;

    this.backgroundMode.enable();
    this.backgroundMode.on('activate').subscribe(() => {
      setTimeout(function () {
        console.log("set timeout triggered");
        self.backgroundMode.moveToForeground();
      }, 5000);
    });
  }
}

当我尝试使用 android studio 调试应用程序时

ionic cap open android -l

我可以在应用程序启动时看到 console.log("application component started"); 的输出,也可以在 5 秒后将应用程序置于后台时看到 console.log("set timeout triggered"); 语句的输出。

但是下面一行

self.backgroundMode.moveToForeground();

没有效果,应用程序保持在后台。

非常感谢任何正确方向的提示。

这是 ionic info 的输出

Ionic:

   Ionic CLI                     : 6.13.1 (/usr/lib/node_modules/@ionic/cli)
   Ionic Framework               : @ionic/angular 5.5.4
   @angular-devkit/build-angular : 0.1101.4
   @angular-devkit/schematics    : 11.1.4
   @angular/cli                  : 11.1.4
   @ionic/angular-toolkit        : 3.1.0

Capacitor:

   Capacitor CLI   : 2.4.6
   @capacitor/core : 2.4.6

Cordova:

   Cordova CLI       : not installed
   Cordova Platforms : not available
   Cordova Plugins   : not available

Utility:

   cordova-res : not installed
   native-run  : 1.3.0

System:

   NodeJS : v15.10.0 (/usr/bin/node)
   npm    : 6.14.11
   OS     : Linux 5.11

0 个答案:

没有答案
相关问题