HTML5+ - 网络及网络状态获取

时间:2019-07-27 发布者: 访问量:3485

Device模块用于获取网络信息

常量:

CONNECTION_UNKNOW: 网络状态常量,表示当前设备网络状态未知,固定值为0。

CONNECTION_NONE: 网络状态常量,当前设备网络未连接网络,固定值为1。

CONNECTION_ETHERNET: 网络状态常量,当前设备连接到有线网络,固定值为2。

CONNECTION_WIFI: 网络状态常量,当前设备连接到无线WIFI网络,固定值为3。

CONNECTION_CELL2G: 网络状态常量,当前设备连接到蜂窝移动2G网络,固定值为4。

CONNECTION_CELL3G: 网络状态常量,当前设备连接到蜂窝移动3G网络,固定值为5。

CONNECTION_CELL4G: 网络状态常量,当前设备连接到蜂窝移动4G网络,固定值为6。

使用getCurrentType函数获取设备当前连接的网络类型

plus.networkinfo.getCurrentType();

演示代码

<div class="mui-content"> <button type="button" id="btn1">获取设备信息</button> </div> </body> <script type="text/javascript"> mui.init(); document.getElementById("btn1").addEventListener('tap', function(){ mui.alert(plus.networkinfo.getCurrentType()); }); </script>


使用网络前建议先判断网络情况

<script type="text/javascript"> mui.init(); document.getElementById("btn1").addEventListener('tap', function(){ var connectionStatus = plus.networkinfo.getCurrentType(); if(connectionStatus == 0 || connectionStatus == 1){ mui.toast('无法连接网络'); }else if(connectionStatus == 3){ mui.toast('使用wifi'); } //...................... }); </script>


检测网络状态变化

<script type="text/javascript"> mui.init(); mui.plusReady(function(){ document.addEventListener("netchange", function(){ var nt = plus.networkinfo.getCurrentType(); switch ( nt ) { case plus.networkinfo.CONNECTION_ETHERNET: case plus.networkinfo.CONNECTION_WIFI: alert("切换到wifi!"); break; case plus.networkinfo.CONNECTION_CELL2G: case plus.networkinfo.CONNECTION_CELL3G: case plus.networkinfo.CONNECTION_CELL4G: alert("切换到4G网络!"); break; default: alert("无网络!"); break; } }, false ); }); </script>

发布于
  用户评论
    生活编程