The*_*לום 9 android ios phonegap-plugins cordova
我一整天都在寻找答案,Google-sphere没有提供任何答案.我已经尝试了所有我知道如何做的事情,并通过建议的解决方案和答案工作,没有任何工作.
概括地说,我试图建立一个Phonegap用于应用程序Android和Apple移动设备和我需要的功能之一是同时检测网络状态和网络连接的类型.以下是我正在使用的代码.
该Firing device ready警报火灾关闭,然后我得到的错误Cannot read property 'type' of undefined,然后通过的循环Navigator对象.当我浏览对象的每个属性时,我没有看到旧版本中使用的connection属性甚至network属性.
有人有主意吗?
的index.html
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<!-- jQuery Core -->
<script src="js/jquery-1.11.1.min.js"></script>
<!-- The main engine for the software. -->
<script src="js/main.js"></script>
<!-- Third party plugins -->
<script type="text/javascript" src="cordova.js"></script>
<script type="text/javascript" src="barcodescanner.js"></script>
<script type="text/javascript" src="childbrowser.js"></script>
<script type="text/javascript" src="js/barcode.js"></script>
<title>index</title>
<script>
document.addEventListener("deviceready", onDeviceReady, false);
</script>
</head>
<body>
<script>
barcode_app.initialize();
</script>
</body>
</html>
Run Code Online (Sandbox Code Playgroud)
main.js
function onDeviceReady(){
alert('Firing device ready');
try{
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.NONE] = 'No network connection';
$("#system_popup").html('Connection type: ' + states[networkState]);
$("#system_popup").popup("open")
//alert('Connection type: ' + states[networkState]);
}catch(e){
alert(e);
$.each(navigator, function(key, value){
alert(key+' => '+value);
});
}
}
Run Code Online (Sandbox Code Playgroud)
在我的config.xml中,我有:
<plugin
name="NetworkStatus"
value="org.apache.cordova.NetworkManager" />
<gap:config-file platform="android" parent="/manifest">
<uses-permission name="android.permission.ACCESS_NETWORK_STATE" />
</gap:config-file>
<gap:config-file platform="android" parent="/manifest">
<uses-permission name="android.permission.INTERNET" />
</gap:config-file>
<gap:config-file platform="android" parent="/manifest">
<uses-permission name="android.permission.READ_PHONE_STATE" />
</gap:config-file>
<feature name="http://api.phonegap.com/1.0/device" />
<feature name="NetworkStatus">
<param name="android-package" value="org.apache.cordova.NetworkManager" />
<param name="ios-package" value="CDVConnection" />
</feature>
Run Code Online (Sandbox Code Playgroud)
更新:解决方案
最终通过@Dawson Loudon和@benka的共同努力制定了解决方案.道森纠正了我正在使用的插件应该是:
<gap:plugin
name="org.apache.cordova.network-information"
version="0.2.7" />
Run Code Online (Sandbox Code Playgroud)
这实际上只有在实现@benka提到的短暂延迟后才能正常工作.所以现在工作代码在JavaScript中看起来像这样:
function onDeviceReady(){
try{
var networkState = navigator.connection && navigator.connection.type;
setTimeout(function(){
networkState = navigator.connection && 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.NONE] = 'No network connection';
alert('Connection type: ' + states[networkState]);
}, 500);
}catch(e){
alert(e);
$.each(navigator, function(key, value){
alert(key+' => '+value);
});
}
}
Run Code Online (Sandbox Code Playgroud)
您想使用此处的插件:http://build.phonegap.com/plugins/626
<gap:plugin name="org.apache.cordova.network-information" version="0.2.7" />
Run Code Online (Sandbox Code Playgroud)