我希望能够根据网络状态更新离子页面。正在调用该函数,但移动设备中的插头网络状态错误进入深度睡眠状态并唤醒。网络在手机进入深度睡眠之前是在线的,当移动深度睡眠时网络应该断开,当移动唤醒时网络应该连接。
constructor(public http: HttpClient,
private platform: Platform,
private network:Network,
private file: File) {
console.log('Hello CommonSdkProvider Provider');
this.disconnected =false;
platform.ready().then(() => {
// Okay, so the platform is ready and our plugins are available.
// Here you can do any higher level native things you might need.
this.file.createFile(this.file.externalDataDirectory,"11122333.txt",true).then((selfmdule)=>{
var result = JSON.parse(JSON.stringify(selfmdule));
console.log("****ok****");
console.log(result);
this.canWritefile =true;
this.getNetWorkConnected();
}).catch((err)=>{
var result = JSON.parse(JSON.stringify(err));
console.log("****fail****");
console.log(result);
this.canWritefile =false;
})
});
}
getNetWorkConnected(){
let iWriteOptions:IWriteOptions
iWriteOptions={replace:false,append:true}
if(this.isMobile()){
let disconnectSubscription = this.network.onDisconnect().subscribe(() => {
console.log('****network was disconnected! *****');
this.disconnected =true;
this.vNetWorkDescriptUnConected = disconnectSubscription;
if(this.canWritefile){
this.file.writeFile(this.file.externalDataDirectory,"11122333.txt","network was disconnected\r\n",iWriteOptions)
}
});
// stop disconnect watch
//disconnectSubscription.unsubscribe();
// watch network for a connection
let connectChangeSubscription = this.network.onConnect().subscribe(() => {
console.log('******network connected!*****');
this.disconnected =false;
this.vNetWorkDescriptConected = connectChangeSubscription;
if(this.canWritefile){
this.file.writeFile(this.file.externalDataDirectory,"11122333.txt","network connected\r\n",iWriteOptions);
}
});
let connectSubscription = this.network.onchange().subscribe((res) => {
console.log(res);
this.disconnected =false;
this.vNetWorkChangeDescriptConected = connectSubscription;
if(this.canWritefile){
this.file.writeFile(this.file.externalDataDirectory,"11122333.txt","network change\r\n",iWriteOptions)
}
});
}
isMobile(): boolean {
return this.platform.is('mobile') && !this.platform.is('mobileweb');
}
结果: 当移动深度睡眠时,网络应该断开,当移动唤醒网络仍然断开时。如果我们在线和离线手动触发网络。状态是正确的。但是在深度睡眠中移动时网络状态会出错。我怎样才能得到正确的结果。 谢谢!