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

给你一囗甜甜゛ 提交于 2020-02-04 19:29:47

来源:netChange问题怎么判断网络状态

  • MUI用于获取当前设备的网络类型

 1 function plusReady(){
 2 
 3    var types = {};
 4 
 5    types[plus.networkinfo.CONNECTION_UNKNOW] = "Unknown connection";
 6 
 7    types[plus.networkinfo.CONNECTION_NONE] = "None connection";
 8 
 9    types[plus.networkinfo.CONNECTION_ETHERNET] = "Ethernet connection";
10 
11    types[plus.networkinfo.CONNECTION_WIFI] = "WiFi connection";
12 
13    types[plus.networkinfo.CONNECTION_CELL2G] = "Cellular 2G connection";
14 
15    types[plus.networkinfo.CONNECTION_CELL3G] = "Cellular 3G connection";
16 
17    types[plus.networkinfo.CONNECTION_CELL4G] = "Cellular 4G connection";
18 
19    alert( "Network: " + types[plus.networkinfo.getCurrentType()] );
20 
21 }
22 
23 if(window.plus){
24 
25   plusReady();
26 
27 }else{
28 
29   document.addEventListener("plusready",plusReady,false);
30 
31 }

 

 

  • MUI的netChange事件

 1 mui.plusReady(function() {
 2 document.addEventListener("netchange",onNetChange,false);
 3 function onNetChange(){
 4   //获取当前网络类型
 5   var nt = plus.networkinfo.getCurrentType();
 6   switch(nt){
 7     case plus.networkinfo.CONNECTION_ETHERNET:
 8     case plus.networkinfo.CONNECTION_WIFI:
 9       mui.toast("当前网络为WiFi");
10       break;
11     case plus.networkinfo.CONNECTION_CELL2G:
12     case plus.networkinfo.CONNECTION_CELL3G:
13     case plus.networkinfo.CONNECTION_CELL4G:
14       mui.toast("当前网络非WiFi");
15       break;
16     default:
17       mui.toast("当前没有网络");
18       break;
19   }
20 }

 

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!