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.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; 
  } 
} 

 

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值