在 phonegap 中创建移动应用程序时,我想显示一条关于互联网连接在线/离线的警告消息。但我没有收到警报消息。我用过
cordova-plugin-network-information
在我的 js 文件中使用下面的代码插件
<script type="text/javascript">
var networkState = navigator.connection.type;
var states = {};
states[Connection.UNKNOWN] = 'Unknown connection';
states[Connection.ETHERNET] = 'Ethernet connection';
states[Connection.WIFI] = 'WiFi connection';
states[Connection.CELL_2G] = 'Cell 2G connection';
states[Connection.CELL_3G] = 'Cell 3G connection';
states[Connection.CELL_4G] = 'Cell 4G connection';
states[Connection.CELL] = 'Cell generic connection';
states[Connection.NONE] = 'No network connection';
alert('Connection type: ' + states[networkState]); </script>
谁能帮我解决这个问题?
最佳答案
首先添加以下 Cordova 插件:
添加以下脚本文件:
jquery.mobile-1.4.0.css
jquery-1.10.2.min.js
cordova.js
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-type" name="viewport"
content="initial-scale=1.0, maximum-scale=1.0, user-scalable=no, width=device-width">
<title></title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<meta name="viewport"
content="width=device-width, initial-scale=1.0, user-scalable=0, minimum-scale=1.0, maximum-scale=1.0">
<meta name="apple-mobile-web-app-capable" content="yes">
<meta name="apple-mobile-web-app-status-bar-style" content="black">
<meta name="format-detection" content="telephone=no">
<link rel="stylesheet" href="css/jquery.mobile-1.4.0.css" />
<script src="cordova.js"></script>
<script type="text/javascript" src="js/jquery-1.10.2.min.js">
</script>
<script>
$(document).ready(function () {
document.addEventListener("deviceready", onDeviceReady, false);
document.addEventListener("online", onOnline, false);
document.addEventListener("offline", onOffline, false);
function onDeviceReady() {
console.log(" ready");
}
function onOnline() {
console.log("connected");
}
function onOffline() {
debugger;
console.log("lost connection");
navigator.notification.alert(
'Please Check your internet connection.', // message
null, // callback
'Sample', // title
'OK' // buttonName
);
}
});
</script>
</head>
<body>
<p style="top:100px;"> Sample</p>
</body>
</html>
输出:
更多详情点击Here..!
关于javascript - 使用 phonegap 检查移动应用程序中的互联网连接,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55586876/