獲取網(wǎng)絡(luò)類(lèi)型
方法一
uni.getNetworkType({
success: function (res) {
console.log(res.networkType);//網(wǎng)絡(luò)類(lèi)型 wifi、2g、3g磨德、4g窒典、ethernet铃绒、unknown插爹、none
if(res.networkType === "none"){
console.log("當(dāng)前無(wú)網(wǎng)絡(luò)");
}else{
console.log("有網(wǎng)絡(luò)");
}
}
});
方法二
getNetworkType() {
var types = {};
types[plus.networkinfo.CONNECTION_UNKNOW] = "Unknown";
types[plus.networkinfo.CONNECTION_NONE] = "None";
types[plus.networkinfo.CONNECTION_ETHERNET] = "Ethernet";
types[plus.networkinfo.CONNECTION_WIFI] = "WiFi";
types[plus.networkinfo.CONNECTION_CELL2G] = "2G";
types[plus.networkinfo.CONNECTION_CELL3G] = "3G";
types[plus.networkinfo.CONNECTION_CELL4G] = "4G";
console.log("Network: " + types[plus.networkinfo.getCurrentType()]);
},
監(jiān)聽(tīng)網(wǎng)絡(luò)狀態(tài)變化
uni.onNetworkStatusChange(function (res) {
console.log(res.isConnected);//當(dāng)前是否有網(wǎng)絡(luò)連接
console.log(res.networkType);//網(wǎng)絡(luò)類(lèi)型
if(res.isConnected === false){
console.log("當(dāng)前無(wú)網(wǎng)絡(luò)");
}else{
console.log("有網(wǎng)絡(luò)");
}
});