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


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值