ionic3项目监听Android物理键返回事件

针对ionic项目的两个模板分别处理。
一、tabs模板项目的物理键返回事件处理机制

import { Component, ViewChild } from '@angular/core';
import {Platform, Nav, IonicApp, ToastController} from 'ionic-angular';
import { StatusBar } from '@ionic-native/status-bar';
import { SplashScreen } from '@ionic-native/splash-screen';

backButtonPressed: boolean = false;
@ViewChild('myNav') nav: Nav;

constructor(public platform: Platform, public statusBar: StatusBar, public splashScreen: SplashScreen, public ionicApp: IonicApp,
              public toastCtrl: ToastController) {
    platform.ready().then(() => {
      // Okay, so the platform is ready and our plugins are available.
      // Here you can do any higher level native things you might need.
      statusBar.styleDefault();
      splashScreen.hide();
      this.registerBackButtonAction(); //注册返回按键事件
      if (this.platform.is('cordova')) {
        this.jpush.init();
        this.jpush.setDebugMode(true);
        this.jpush.setBadge(0);
        this.jpush.setApplicationIconBadgeNumber(0);
        this.getRegistrationID();
      }

    });
  }

  registerBackButtonAction(){
    this.platform.registerBackButtonAction(()=>{
      //如果想点击返回按钮隐藏toast或loading或Overlay就把下面加上
      // this.ionicApp._toastPortal.getActive() || this.ionicApp._loadingPortal.getActive() || this.ionicApp._overlayPortal.getActive()
      let activePortal = this.ionicApp._modalPortal.getActive();
      if (activePortal) {
        activePortal.dismiss().catch(() => {});
        activePortal.onDidDismiss(() => {});
        return;
      }
      let activeVC = this.nav.getActive();
      let tabs = activeVC.instance.tabs;
      let activeNav = tabs.getSelected();
      return activeNav.canGoBack() ? activeNav.pop() : this.showExit();//另外两种方法在这里将this.showExit()改为其他两种的方法的逻辑就好。
    },1);
  }

  //双击退出提示框
  showExit() {
    if (this.backButtonPressed) { //当触发标志为true时,即2秒内双击返回按键则退出APP
      this.platform.exitApp();
    } else {
      this.toastCtrl.create({
        message: '再按一次退出应用',
        duration: 2000,
        position: 'middle'
      }).present();
      this.backButtonPressed = true;
      setTimeout(() => this.backButtonPressed = false, 2000);//2秒内没有再次点击返回则将触发标志标记为false
    }
    // var that = this;
    // this.ionicService.presentConfirm('退出提示','确定要退出应用程序吗?',function () {
    //   that.platform.exitApp();
    // })

  }

二、ionic sideMenu模板项目的物理键返回事件处理机制

import { Component, ViewChild } from '@angular/core';
import {Nav, Platform, IonicApp, ToastController, NavController, App, Tabs} from 'ionic-angular';
import { StatusBar } from '@ionic-native/status-bar';
import { SplashScreen } from '@ionic-native/splash-screen';


export class MyApp {
  @ViewChild(Nav) nav: Nav;

  backButtonPressed: boolean = false;

  constructor(public platform: Platform, public statusBar: StatusBar, public splashScreen: SplashScreen, public ionicApp: IonicApp,public toastCtrl: ToastController, public app:App) {
    this.initializeApp()
  }

  initializeApp() {
    this.platform.ready().then(() => {
      // Okay, so the platform is ready and our plugins are available.
      // Here you can do any higher level native things you might need.
      this.statusBar.styleDefault();
      this.splashScreen.hide();
      this.registerBackButtonAction(Tabs); //注册返回按键事件
    });
  }

  registerBackButtonAction(tabRef){
    this.platform.registerBackButtonAction(() => {
      //获取NavController
      let activeNav: NavController = this.app.getActiveNav();

      // 有博主说上面的方法在新的版本中被移除,但是我在测试的时候还可以继续使用,下面这段代码是新的使用方式,我也贴出来。
      // let activeNav: NavController = this.appCtrl.getActiveNavs()[0];

      //如果可以返回上一页,则执行pop
      if (activeNav.canGoBack()) {
        activeNav.pop();
      } else {
        // if (tabRef == null || tabRef._selectHistory[tabRef._selectHistory.length - 1] === tabRef.getByIndex(0).id) {
        //   //执行退出
        //   this.showExit();
        // } else {
        //   //选择首页第一个的标签
        //   tabRef.select(0);
        // }
        this.showExit();
      }
    });
  }

  //双击退出提示框
  showExit() {
    if (this.backButtonPressed) { //当触发标志为true时,即2秒内双击返回按键则退出APP
      this.platform.exitApp();
    } else {
      this.toastCtrl.create({
        message: '再按一次退出应用',
        duration: 2000,
        position: 'middle'
      }).present();
      this.backButtonPressed = true;
      setTimeout(() => this.backButtonPressed = false, 2000);//2秒内没有再次点击返回则将触发标志标记为false
    }
  }
}

可以针对不同的项目情况,做相应的调整。

你可能感兴趣的:(ionic3项目监听Android物理键返回事件)