我正在检查nativescript中的Internet连接。我写了以下代码:
startMonitoring(): void {
connectivity.startMonitoring((newConnectionType: number) => {
switch (newConnectionType) {
case connectivity.connectionType.none:
this.connectionType = "None";
console.log("Connection type changed to none.");
break;
case connectivity.connectionType.wifi:
this.connectionType = "Wi-Fi";
console.log("Connection type changed to WiFi.");
break;
case connectivity.connectionType.mobile:
this.connectionType = "Mobile";
console.log("Connection type changed to mobile.");
break;
default:
break;
}
});
}
,并在Android Manifiedt上添加了权限,如下所示:
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
然后,当我运行 tns debug android 时,我仍然收到错误消息:
JS:错误错误:java.lang.SecurityException:ConnectivityService: 用户10224或当前进程都没有 android.permission.ACCESS_NETWORK_STATE。 JS:
android.os.Parcel.readException(Parcel.java:1954)JS:
android.os.Parcel.readException(Parcel.java:1900)JS:
android.net.IConnectivityManager $ Stub $ Proxy.getActiveNetworkInfo(IConnectivityManager.java:1216) JS:
android.net.ConnectivityManager.getActiveNetworkInfo(ConnectivityManager.java:903) JS:com.tns.Runtime.callJSMethodNative(本地方法)JS:
com.tns.Runtime.dispatchCallJSMethodNative(Runtime.java:1203)JS:
com.tns.Runtime.callJSMethodImpl(Runtime.java:1083)JS:
com.tns.Runtime.callJSMethod(Runtime.java:1070)JS:
com.tns.Runtime.callJSMethod(Runtime.java:1050)JS:
com.tns.Runtime.callJSMethod(Runtime.java:1042)JS:
com.tns.gen.android.content.BroadcastReceiver_vendor_119374_32_BroadcastReceiver.onReceive(BroadcastReceiver_vendor_119374_32_BroadcastReceiver.java:19) JS:
android.app.LoadedApk $ ReceiverDispatcher $ Args.lambda $ -android_app_LoadedApk $ ReceiverDisp ...
奇怪的是,即使我已经添加了权限,仍然出现错误。为什么会这样?
PS:还尝试了 tns运行android ,但有相同的错误。
谢谢!