html5+常用功能

这篇博文介绍了html5+在webApp开发中经常使用的功能模块

获取wifi信息

  1. 获取wifi的IP

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    function getOnuIp() {
    if(mui.os.android) {
    var Context = plus.android.importClass("android.content.Context");
    var WifiManager = plus.android.importClass("android.net.wifi.WifiManager");
    var WifiInfo = plus.android.importClass("android.net.wifi.WifiInfo");

    var wifiManager = plus.android.runtimeMainActivity().getSystemService(Context.WIFI_SERVICE);
    var wifiInfo = wifiManager.getConnectionInfo();
    var ipAddress = wifiInfo.getIpAddress();
    if(ipAddress == 0) return "未连接wifi";
    return((ipAddress & 0xff) + "." + (ipAddress >> 8 & 0xff) + "." + (ipAddress >> 16 & 0xff) + "." + (ipAddress >> 24 & 0xff));
    } else if(mui.os.ios) {
    mui.toast("ios暂未获");
    }
    }
  2. 获取wifi的网关

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    function getOnuIp() {
    if(mui.os.android) {
    var Context = plus.android.importClass("android.content.Context");
    var WifiManager = plus.android.importClass("android.net.wifi.WifiManager");
    var WifiInfo = plus.android.importClass("android.net.wifi.WifiInfo");
    var DhcpInfo=plus.android.importClass("android.net.DhcpInfo");
    var wifiManager = plus.android.runtimeMainActivity().getSystemService(Context.WIFI_SERVICE);
    var dhcpInfo = wifiManager.getDhcpInfo()
    var begin=dhcpInfo.toString().indexOf("gateway");
    var end=dhcpInfo.toString().indexOf("netmask");
    var ipAddress = dhcpInfo.toString().substring(begin+7,end).trim();
    if(ipAddress == 0) return "未连接wifi";
    return ipAddress;
    // return((ipAddress & 0xff) + "." + (ipAddress >> 8 & 0xff) + "." + (ipAddress >> 16 & 0xff) + "." + (ipAddress >> 24 & 0xff));
    } else if(mui.os.ios) {
    var WifiManager = plus.ios.importClass("WifiManager");
    var wifiManager = new WifiManager();
    var gateWay = wifiManager.defaultGateWay();
    return gateWay;
    }
    }

软键盘

  1. 强制打开全键盘

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    //弹出软键盘,搜索框获得焦点
    var nativeWebview, imm, InputMethodManager;
    var initNativeObjects = function() {
    if(mui.os.android) {
    var main = plus.android.runtimeMainActivity();
    var Context = plus.android.importClass("android.content.Context");
    InputMethodManager = plus.android.importClass("android.view.inputmethod.InputMethodManager");
    imm = main.getSystemService(Context.INPUT_METHOD_SERVICE);
    } else {
    nativeWebview = plus.webview.currentWebview().nativeInstanceObject();
    }
    };
    var showSoftInput = function() {
    if(mui.os.android) {
    imm.toggleSoftInput(0, InputMethodManager.SHOW_FORCED);
    } else {
    nativeWebview.plusCallMethod({
    "setKeyboardDisplayRequiresUserAction": false
    });
    }
    setTimeout(function() {
    //此处可写具体逻辑设置获取焦点的input
    var inputElem = document.querySelector('input');
    inputElem.focus();
    }, 200);
    };
    var showSoftInput2 = function() {
    var nativeWebview = plus.webview.currentWebview().nativeInstanceObject();
    if(mui.os.android) {
    //强制当前webview获得焦点
    plus.android.importClass(nativeWebview);
    nativeWebview.requestFocus();
    imm.toggleSoftInput(0, InputMethodManager.SHOW_FORCED);
    } else {
    nativeWebview.plusCallMethod({
    "setKeyboardDisplayRequiresUserAction": false
    });
    }
    setTimeout(function() {
    //此处可写具体逻辑设置获取焦点的input
    var inputElem = document.querySelector('input');
    inputElem.select();
    }, 200);
    };
  2. 关闭软键盘

    1
    document.activeElement.blur(); //隐藏软键盘

两次点击退出

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
//首页返回键处理  
//处理逻辑:2秒内,连续两次按返回键,则退出应用
//首次按键,提示‘再按一次退出应用’
if(!first) {
first = new Date().getTime();
mui.toast('再按一次退出应用');
setTimeout(function() {
first = null;
}, 2000);
return false;
} else {
if(new Date().getTime() - first < 2000) {
plus.runtime.quit();
}
}

注销关闭其他窗口

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
/*监听注销按钮*/
document.getElementById("logoutBtn").addEventListener("tap", function() {
var btnArray = ['是', '否'];
mui.confirm('您确定退出?', '', btnArray, function(e) {
if(e.index == 0) {
// 获取所有Webview窗口
var launchWebview = plus.webview.getLaunchWebview();
var curr = plus.webview.currentWebview();
var wvs = plus.webview.all();
for(var i = 0, len = wvs.length; i < len; i++) {
//关闭除setting页面和首页外的其他页面
if(wvs[i].getURL() == curr.getURL() || wvs[i].getURL() == launchWebview.getURL())
continue;
plus.webview.close(wvs[i]);
}
//清除已存储的账号信息
myStorage.removeItem('ONU.token.username');
myStorage.removeItem('ONU.token.password');
myStorage.removeItem('ONU.token.autoLogin');
myStorage.removeItem('isLogin');
//注销静默登录的定时器
//clearInterval('backLoading()');
//打开login页面后再关闭setting页面
launchWebview.show();
curr.close();
}
})
});
坚持原创技术分享,您的支持将鼓励我继续创作!
0%