[关闭]
@MrXiao 2017-05-19T10:07:59.000000Z 字数 3758 阅读 731

html5+常用功能

html5 native.js


获取wifi信息

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

软键盘

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

两次点击退出

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

注销关闭其他窗口

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