当前位置: 移动技术网 > IT编程>网页制作>Html5 > HTML5下的网络及网络状态获取讲解

HTML5下的网络及网络状态获取讲解

2018年09月12日  | 移动技术网IT编程  | 我要评论

查看详细信息

networkinfo 获取网络信息

常量:

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()
获取当前设备连接的网络类型,返回值为网络类型常量,可取值connection_*常量。
返回值:
number : 设备当前网络类型

判断网络情况
var connectionstatus = plus.networkinfo.getcurrenttype();
if(connectionstatus == 0 || connectionstatus == 1){
    mui.toast('无法连接网络');
}else if(connectionstatus == 3){
    mui.toast('使用wifi');
}else{
    ........
}

netchange  检测网络状态变化
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(""); 
            break; 
            case plus.networkinfo.connection_cell2g:
            case plus.networkinfo.connection_cell3g:
            case plus.networkinfo.connection_cell4g:
            alert(""); 
            break; 
            default:
            alert("无网络!"); 
            break;
        }
    }, false );
});

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网