我正在尝试创建一个线程来不断检查应用程序的网络状态,以便它可以发送到我的远程数据库。我如何在ionic 2中使用Ionic-native来处理这个问题。
诚挚的问候
发布于 2017-02-07 02:31:58
首先,你需要安装网络插件
ionic plugin add cordova-plugin-network-information
mor info here
然后你需要一个提供商来检查网络状态
import { Injectable } from '@angular/core';
import { Platform } from 'ionic-angular';
import { Network } from 'ionic-native';
@Injectable()
export class NetworkService {
onDevice: boolean;
constructor(
private platform: Platform,
) {
this.onDevice = this.platform.is('cordova');
}
isOnline(): boolean {
if (this.onDevice && Network.type !== 'none') {
return true;
} else {
return navigator.onLine;
}
}
}
现在你可以在任何你喜欢的地方使用它
发布于 2017-02-08 22:16:07
如果您遵循以下步骤:
您将能够注册随Ionic 2应用程序提供的service-worker.js。
您可以在此处找到有关服务工作者如何工作的更多文档:
https://developers.google.com/web/fundamentals/getting-started/primers/service-workers
您可能需要将@armin的响应与此响应结合使用。
安装:
ionic plugin add cordova-plugin-network-information
然后:
使用如下代码(此代码需要添加到您的ionic应用程序中,而不是您的service-worker.js中):
import { Network } from 'ionic-native';
//watch network for a disconnect
let disconnectSubscription = Network.onDisconnect().subscribe(() => {
console.log('network was disconnected :-(');
// Warn your service worker about this and create a queue or
//something to notify your server when you get connection again.
});
//watch network for a connection
let connectSubscription = Network.onConnect().subscribe(() => {
console.log('network connected!');
// We just got a connection but we need to wait briefly
// Notify to your service-worker.js about this and hit your server.
});
我认为你想要这样的东西。
https://stackoverflow.com/questions/42068470
复制相似问题