在项目中需要使用connection.getAllNetsSync()判断当前可用网络,已经在Entry的module.json5中声明了权限ohos.permission.GET\_NETWORK\_INFO,但是仍然抛出异常201 - Permission denied
"name": "ohos.permission.GET_NETWORK_INFO",
"reason": "$string:reason",
"usedScene": {
"abilities": [
"EntryFormAbility"
],
"when": "inuse"
}
/**
* 判断当前是否存在可用网络
* netId:
* 101 - wifi
* 102 - 蜂窝网
* @returns
*/
public isNetworkEnable(): boolean {
try {
let nets = connection.getAllNetsSync();
return nets !== undefined && nets.length > 0;
} catch (e) {
Log.error('isNetworkEnable error ' + JSON.stringify(e))
return false;
}
}
可以正常获取的,请将修改"abilities": ["EntryFormAbility"],为"abilities": ["EntryAbility"],后重试。