ionic4监听返回事件 AppMinimize navController

引入appminimize 插件
 ionic cordova plugin add cordova-plugin-appminimize
 npm install --save @ionic-native/app-minimize@beta
 在 app.module.ts 中注入 appminimize 并且在providers 中注入
 import {
    
    AppMinimize} from '@ionic-native/app-minimize/ngx';
![在这里插入图片描述](https://img-blog.csdnimg.cn/20200922201153262.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3FxXzMxMTgyMzk5,size_16,color_FFFFFF,t_70#pic_center)
在 app.components.ts 监听
import {
    
    Component, } from '@angular/core';
import {
    
     NavController } from '@ionic/angular';
import {
    
    Platform, ToastController} from '@ionic/angular';
import {
    
    Subscription} from 'rxjs';
import {
    
    SplashScreen} from '@ionic-native/splash-screen/ngx';
import {
    
    StatusBar} from '@ionic-native/status-bar/ngx';
import {
    
    NavigationEnd, Router} from '@angular/router';
import {
    
    AppMinimize} from '@ionic-native/app-minimize/ngx';
@Component({
    
    
  selector: 'app-root',
  templateUrl: 'app.component.html',
  styleUrls: ['app.component.scss']
})
export class AppComponent{
    
    
  backButtonPressed = false; // 用于判断返回键是否触发
  customBackActionSubscription: Subscription;
  url;

  constructor(
      private platform: Platform,
      private router: Router,
      private minimize: AppMinimize,
      public toastController: ToastController,
      private splashScreen: SplashScreen,
      private statusBar: StatusBar,
      private nav: NavController
  ) {
    
    
      this.initRouterListen();
      this.initializeApp();
  }

  initializeApp() {
    
    
      this.platform.ready().then(() => {
    
    
        // this.statusBar.styleDefault();
        this.statusBar.overlaysWebView(false);
        this.statusBar.backgroundColorByHexString('#3880f');
        this.splashScreen.hide();
        this.registerBackButtonAction();
      });
  }

  registerBackButtonAction() {
    
    
      this.customBackActionSubscription = this.platform.backButton.subscribe(() => {
    
    
          if (this.url === '/tabs'
              || this.url === '/tabs/tab1'
              || this.url === '/tabs/tab2'
              || this.url === '/tabs/tab3') {
    
     // 监测到当前路由,判断是否要退出程序
              if (this.backButtonPressed) {
    
    
                  //  this.minimize.minimize(); 程序最小化
                  // tslint:disable-next-line: no-string-literal
                  navigator[ 'app' ].exitApp();
                  this.backButtonPressed = false;
              } else {
    
    
                  this.miniApp(); // 提示toast
                  this.backButtonPressed = true;
                  setTimeout(() => this.backButtonPressed = false, 2000);
              }
          }else{
    
    
            this.nav.back();
          }
      });
  }

  initRouterListen() {
    
    
      this.router.events.subscribe(event => {
    
     // 需要放到最后一个执行
          if (event instanceof NavigationEnd) {
    
    
              this.url = event.url;
              console.log(this.url);
          }
      });
  }

  async miniApp() {
    
    
      const toast = await this.toastController.create({
    
    
          message: '再按一次退出应用',
          duration: 2000,
          position: 'middle'
      });
      toast.present();
  }
}


猜你喜欢

转载自blog.csdn.net/qq_31182399/article/details/108739972