2017-10-19 23 views
0

J'ai fait une fonction qui détecte l'état du réseau et tout fonctionne bien lorsque je tester cette fonction sur le navigateur ou l'application vue ionique.Détection de réseau de Ionic 2 ne fonctionne pas correctement

Mon problème est survenu lorsque j'ai installé mon application sur mon appareil Android, j'ai éteint mon wifi mais rien ne se passe.

Voici mon code:

constructor(...){ 
    platform.ready().then(() => { 
     ... 
     this.checkConnection(); 
    }) 
} 

checkConnection(){ 
    this.network.onConnect().subscribe(data => { 
     console.log(data); 
     //this.displayNetworkUpdate(data.type); 
     if(this.isOffline==true){ 
      this.isOffline=false; 
      this.toast.dismiss(); 
     } 
    }, error => console.error(error)); 
    this.network.onDisconnect().subscribe(data => { 
     console.log(data) 
     //this.displayNetworkUpdate(data.type); 
     if(this.isOffline==false){ 
      this.isOffline=true; 
      this.toast= this.toastCtrl.create({ 
       message: 'No Internet connection. Make sure that Wi-Fi or Cellular mobile data is turned on, then continue using the application.', 
       duration: 99999999999 
      }); 
      this.toast.present(); 
     } 
    }, error => console.error(error)); 
} 

Répondre

0

Ma compréhension est que ces Observables sont les changements d'état. Voyez cette solution que j'ai créée.

fournisseurs/LastKnownNetworkState.ts

import {Injectable} from '@angular/core'; 
import {Network} from '@ionic-native/network'; 


@Injectable() 
export class LastKnownNetworkState { 
    online: boolean; 

    constructor(private network: Network) { 
    // can get the initial state. 
    this.online = navigator.onLine; 


    // these will capture change in state 
    this.network.onDisconnect().subscribe(() => { 
     this.online = false; 
    }); 
    this.network.onConnect().subscribe(() => { 
     this.online = true; 
    }); 

    } 

    isOnline(): boolean { 
    return this.online; 
    } 

}