首页 > 建站教程 > APP开发,混合APP >  MUI判断网络连接以及监听网络变化JS正文

MUI判断网络连接以及监听网络变化JS

MUI用于获取当前设备的网络类型
function plusReady(){
   var types = {};
   types[plus.networkinfo.CONNECTION_UNKNOW] = "Unknown connection";
   types[plus.networkinfo.CONNECTION_NONE] = "None connection";
   types[plus.networkinfo.CONNECTION_ETHERNET] = "Ethernet connection";
   types[plus.networkinfo.CONNECTION_WIFI] = "WiFi connection";
   types[plus.networkinfo.CONNECTION_CELL2G] = "Cellular 2G connection";
   types[plus.networkinfo.CONNECTION_CELL3G] = "Cellular 3G connection";
   types[plus.networkinfo.CONNECTION_CELL4G] = "Cellular 4G connection";
   alert( "Network: " + types[plus.networkinfo.getCurrentType()] );
}
if(window.plus){
  plusReady();
}else{
  document.addEventListener("plusready",plusReady,false);
}
MUI的netChange网络类型改变事件
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:
       mui.toast("当前网络为WiFi");
       break;
     case plus.networkinfo.CONNECTION_CELL2G:
     case plus.networkinfo.CONNECTION_CELL3G:
     case plus.networkinfo.CONNECTION_CELL4G:
       mui.toast("当前网络非WiFi");
       break;
     default:
       mui.toast("当前没有网络");
       break;
   }
 }
}