连接模块使用Nativescript不起作用

问题描述 投票:0回答:1

我按照this教程,用app.component.ts写这段代码。

    export class AppComponent implements OnInit {
    isConnection: boolean;
    constructor() {}
    ngOnInit() {
        const myConnectionType = getConnectionType();
        switch (myConnectionType) {
            case connectionType.none:
                this.isConnection= false;
                dialogs.confirm({
                    message: "Please, check Wifi",
                    okButtonText: "OK",
                }).then(result => {
                    console.log("Dialog result: " + result);
                });
                break;
            case connectionType.wifi:
                this.isConnection= true
                break;
            case connectionType.mobile:
                this.isConnection= false;
                dialogs.confirm({
                    message: "Please, check Wifi",
                    okButtonText: "OK",
                }).then(result => {
                    console.log("Dialog result: " + result);
                });
                break;
            case connectionType.ethernet:
                this.isConnection= false;
                dialogs.confirm({
                    message: "Please, check Wifi",
                    okButtonText: "OK",
                }).then(result => {
                    console.log("Dialog result: " + result);
                });
                break;
            default:
                break;
        }
    }
}

我不明白为什么当我断开WiFi时无法正常工作?

在app.component.html中

<page-router-outlet></page-router-outlet>

在AndroidManifest / xml中我放了<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>

你有什么想法吗?

android typescript nativescript multipeer-connectivity
1个回答
1
投票

回答你的问题“当我断开WiFi时,为什么不起作用?”我们“You have to monitor the connection”。在您的代码中,您只是检查应用程序加载时的连接类型,但是您还必须监视它,以防您想要检查wi-fi何时被dicsonnected。

ngOnInit() {
connectivity.startMonitoring((newConnectionType: number) => {
      switch (newConnectionType) {
        case connectivity.connectionType.none:
          this._userService.connectionType = AppConstants.INT_CONN_NONE;
          console.log('Connection type changed to none.');
          break;
        case connectivity.connectionType.wifi:
          this._userService.connectionType = AppConstants.INT_CONN_WIFI;
          console.log('Connection type changed to WiFi.');
          break;
        case connectivity.connectionType.mobile:
          this._userService.connectionType = AppConstants.INT_CONN_MOBILE;
          console.log('Connection type changed to mobile.');
          break;
        default:
          break;
      }
    });
}
© www.soinside.com 2019 - 2024. All rights reserved.