不是將checkConnection函數放在index.html中,而是將它添加到onDeviceReady中的index.js中。
document.addEventListener('deviceready', onDeviceReady.bind(this), false);
function onDeviceReady() {
checkConnection();
};
function checkConnection(){
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(states[networkState]);
}
這應該有效。
我做了一個基本的演示,但沒有轉載問題。你可以在一個空白的空白項目中試試嗎? –