Phonegap / android / html如果连接到localhost

时间:2014-03-15 10:38:18

标签: javascript android html5 cordova

我想确定我的移动设备如何连接到互联网(VIA)家庭路由器或3G或其他路由器。根据使用的连接,我想执行更多任务。 感谢

1 个答案:

答案 0 :(得分:1)

您可以参考完整文档here

(in app/res/xml/config.xml)

<feature name="NetworkStatus">
    <param name="android-package" value="org.apache.cordova.NetworkManager" />
</feature>

(in app/AndroidManifest.xml)

<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />

代码段:

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('Connection type: ' + states[networkState]);
}

checkConnection();