android - 连接模块无法使用 Nativescript
问题描述
我遵循本教程,并在 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"/>
请问你有什么想法吗?
解决方案
回答您的问题“为什么我断开 WiFi 时不起作用?” 我们“ You have to monitor the connection
”。在您的代码中,您只是检查应用程序加载时的连接类型,但您还必须对其进行监控,以防您想要检查何时断开 wi-fi。
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;
}
});
}
推荐阅读
- validation - Sweetalert2 多输入验证
- c# - 表不使用 DateTimePicker 过滤按钮单击
- mongodb - Docker 中的 Mongo + 播种机
- rest - 在 Spring Boot 中避免在 rest 控制器中出现相同的 post 请求
- java - 带队列的 RoundRobin 算法
- java - Proguard 保持枚举常量名称
- sql - Spark中聚合时的条件
- angular - 如何在 mat-tab 中的活动选项卡上触发功能
- python - 访问 pd.TimeSeries 的频率属性
- python - 如何使用 beautifulsoup 检查字符串是否存在