Android 如何在不改变离子4状态的情况下检查连接?

Android 如何在不改变离子4状态的情况下检查连接?,android,angular,ionic-framework,ionic4,Android,Angular,Ionic Framework,Ionic4,我想创建一个应用程序,该应用程序需要连接到internet,我想检查internet连接而不更改网络状态,我使用library,谢谢。如果您想在组件(页面)中侦听网络状态,我建议您在页面生命周期事件中执行此操作,如IonViewDiLoad()或IonViewDiCenter()。通常,您希望确保构造函数处理的逻辑尽可能少,因为它只会触发一次。你可以阅读更多关于他们的信息 在我的应用程序中,我正在侦听作为HTTP模块包装器的提供者中的网络状态。为了让它工作,我必须初始化platform.read

我想创建一个应用程序,该应用程序需要连接到internet,我想检查internet连接而不更改网络状态,我使用library,谢谢。

如果您想在组件(页面)中侦听网络状态,我建议您在页面生命周期事件中执行此操作,如IonViewDiLoad()或IonViewDiCenter()。通常,您希望确保构造函数处理的逻辑尽可能少,因为它只会触发一次。你可以阅读更多关于他们的信息

在我的应用程序中,我正在侦听作为HTTP模块包装器的提供者中的网络状态。为了让它工作,我必须初始化platform.ready()块中的所有逻辑,以确保设备实际上已经准备好开始被操作。以下是我的提供商的样子:

export class HTTP {
  public online:boolean = true;
  public base:string; // this will be set in the constructor based on if we're in dev or prod
  public token:string = null;

  constructor(public platform:Platform, public http: Http, public events:Events, public cache:CacheService, private network:Network) {
    if(document.URL.includes('https://') || document.URL.includes('http://')){
      this.base = "http://127.0.0.1:3001/";
    } else {
      this.base = "https://url.to_actual_URL.com/";
    }

    this.platform.ready().then(() => {
      let type = this.network.type;

      //console.log("Connection type: ", this.network.type);
      // Try and find out the current online status of the device
      if(type == "unknown" || type == "none" || type == undefined){
        //console.log("The device is not online");
        this.online = false;
      }else{
        //console.log("The device is online!");
        this.online = true;
      }
    });

    this.network.onDisconnect().subscribe( () => {
      this.online = false;
      //console.log('network was disconnected :-(');
    });

    this.network.onConnect().subscribe( () => {
      this.online = true;
      //console.log('network was connected :-)');
    });
  }
}