当前位置 : 主页 > 手机开发 > cordova >

cordova 3.0:Android:没有定义连接

来源:互联网 收集:自由互联 发布时间:2021-06-10
我第一次尝试使用Apache Cordova 3.0. 下载了lib,解压缩了cordova-android和cordova-js并创建了一个项目: ./create ~/Documents/andriod-projects/HelloWorld com.x.HelloWorld HelloWorld- OK RES / XML / config.xml中 plugin n
我第一次尝试使用Apache Cordova 3.0.

下载了lib,解压缩了cordova-android和cordova-js并创建了一个项目:

./create ~/Documents/andriod-projects/HelloWorld com.x.HelloWorld HelloWorld
- OK

RES / XML / config.xml中

<plugin name="NetworkStatus" value="org.apache.cordova.NetworkManager" />

AndroidManifest.xml中

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

在index.js设备上准备好了:

bindEvents: function() {
    document.addEventListener('deviceready', this.onDeviceReady, false);
},

onDeviceReady: function() {

    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("Network: "+states[networkState]);
}

当我在我的orriod上模拟项目时,我遇到了LogCat错误:连接未定义:

我失踪了我要附加一个.js为了连接被声明?

Uncaught Refference error: Connection is not defined

与缺少“连接”对象有关,根据我对corodva 3.1.0的体验,即使在Benka提出的延迟之后,它也不可用.通过使用navigator.connection对象的常量可以解决这个特殊问题,如下所示:

var states = {};
states[navigator.connection.UNKNOWN]  = 'Unknown connection';
states[navigator.connection.ETHERNET] = 'Ethernet connection';
states[navigator.connection.WIFI]     = 'WiFi connection';
states[navigator.connection.CELL_2G]  = 'Cell 2G connection';
states[navigator.connection.CELL_3G]  = 'Cell 3G connection';
states[navigator.connection.CELL_4G]  = 'Cell 4G connection';
states[navigator.connection.CELL]     = 'Cell generic connection';
states[navigator.connection.NONE]     = 'No network connection';

不幸的是,在我的情况下,这只是Android的网络状态的问题的开始

navigator.connection.type

将始终返回0,这是未知连接.两者都在android模拟器和一个设备上.适用于我的解决方法是直接调用插件类:

document.addEventListener("deviceready", onDeviceReady, false);
function onDeviceReady() {
    var conn = checkConnection();
    alert("Connection:"+conn);


}
function checkConnection(){
        var networkState;
        var test = cordova.exec(
                function(winParam) {networkState = winParam;},
                function(error) {alert("Network Manager error: "+error);},
                "NetworkStatus",
                "getConnectionInfo",
                []
        );
        return networkState;
}

这个代码在一个函数内部有一个丑陋的networkState赋值,可能会在checkConnection返回语句之后异步执行,但是当本地代码在执行函数内返回PluginResult时,这可以工作.返回的networkState值与navigator.connection不匹配.常数如:

navigator.connection.WIFI

您可以在这里看到插件源代码中返回的值:https://github.com/apache/cordova-plugin-network-information/blob/master/src/android/NetworkManager.java

网友评论