import { Injectable } from '@angular/core'; import {Network} from '@ionic-native/network/ngx'; import {AlertController} from '@ionic/angular'; @Injectable() export class NetworkService { networkRemind = true; constructor(private alertCtrl: AlertController, private network: Network) {} async checkNetworkType() { // 检测网络连接,如果是移动数据网络,则提醒用户 if (this.networkRemind && (this.network.type === '3g' || this.network.type === '4g')) { const confirm = await this.alertCtrl.create({ header: '检测到网络连接变化!', message: '您现在使用的是' + this.network.type + ',继续使用将会产生流量费用,是否继续?', buttons: [ { text: '否', handler: () => { this.networkRemind = true; } }, { text: '是,将不再提醒网络变化', handler: () => { this.networkRemind = false; } } ] }); await confirm.present(); } this.network.onConnect().subscribe(() => { setTimeout(async () => { if (this.networkRemind && (this.network.type === '3g' || this.network.type === '4g')) { const confirm = await this.alertCtrl.create({ header: '检测到网络连接变化!', message: '您现在使用的是' + this.network.type + ',继续使用将会产生流量费用,是否继续?', buttons: [ { text: '否', handler: () => { this.networkRemind = true; } }, { text: '是,将不再提醒网络变化', handler: () => { this.networkRemind = false; } } ] }); await confirm.present(); } }, 3000); }); } }