引入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';

在 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.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) {
navigator[ 'app' ].exitApp();
this.backButtonPressed = false;
} else {
this.miniApp();
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();
}
}