我试图在我的Android应用中检查网络是否可用。
我的代码
config.xml中
<?xml version='1.0' encoding='utf-8'?>
<widget id="com.testback.build" version="1.0.2" xmlns="http://www.w3.org/ns/widgets" xmlns:cdv="http://cordova.apache.org/ns/1.0">
<icon src="assets/images/logo12.png" />
<name>TestNetwork</name>
<description>
Desc for network check
</description>
<content src="index.html" />
<access origin="*" />
<preference name="permissions" value="none"/>
<feature name="http://api.phonegap.com/1.0/network"/>
<feature name="NetworkStatus">
<param name="android-package" value="org.apache.cordova.networkinformation.NetworkManager" />
</feature>
</widget>
在Js
document.addEventListener("deviceready", onDeviceReady, false);
function onDeviceReady()
{
checkConnection();
}
function checkConnection() {
try{
var networkState = navigator.connection && navigator.connection.type;
alert(networkState); // returns undefined
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);
}
}
配置文件中是否包含任何参数。我使用的是phonegap 3.6.3