私の代わりにイベント(https://ionicframework.com/docs/v2/api/util/Events/)のために行くことにしました:
constructor(private events: Events, private alertCtrl: AlertController) {
this.platform.ready().then(() => {
this.watchForNetworkChanges();
});
watchForNetworkChanges() {
// Watch network for a connection
Network.onConnect().subscribe(() => {
this.events.publish('network:connected', true);
});
Network.onDisconnect().subscribe(() => {
this.events.publish('network:connected', false);
});
そして、私はネットワークの状態変化に対処するために、これらのイベントをサブスクライブし、他の場所で:
this.events.subscribe('network:connected', (status) => {
let connected = status[0] === true;
if (!connected) {
let alert = this.alertCtrl.create({
title: 'No Internet',
message: "You are offline!"
buttons: ['OK']
});
alert.present();
}
this.isConnected = connected;
}
出典
2016-11-29 14:22:38
AFD
は、このコードは動作しませんでした...それがお役に立てば幸い){ console.log( "ONLINE"); connectivity.callServer(); }、false); –
私は両方のシミュレータと実際のアンドロイドデバイスでこのコードをテストしています。どのプラットフォームでテストしましたか? –
はChromeでテストされています... –