//获取当前设备的网络类型
function plusReady() {
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";
if (types[plus.networkinfo.getCurrentType()] == "None") {
console.log('暂无网络')
} else {
console.log('有网络')
}
}
if (window.plus) {
plusReady();
} else {
document.addEventListener("plusready", plusReady, false);
}
//监听网络切换
mui.plusReady(function() {
document.addEventListener("netchange", onNetChange, false);
//获取当前网络类型
function onNetChange() {
var nt = plus.networkinfo.getCurrentType();
switch (nt) {
case plus.networkinfo.CONNECTION_ETHERNET:
case plus.networkinfo.CONNECTION_WIFI:
case plus.networkinfo.CONNECTION_CELL2G:
case plus.networkinfo.CONNECTION_CELL3G:
case plus.networkinfo.CONNECTION_CELL4G:
console.log('当前有网络');
break;
default:
console.log('当前没有网络');
break;
}
}
});