Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
215 views
in Technique[技术] by (71.8m points)

phonegap plugins - cordova 3.0: Android: Connection is not defined

my first time experimenting with Apache Cordova 3.0.

downloaded lib, unziped cordova-android and cordova-js and created a project:

./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" />

on index.js device ready:

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]);
}

when I emulate the project on my andriod I got in LogCat Error:Connection is not defined:

enter image description here

what I am missing? I have to attach a .js in order to Connection be declared?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Uncaught Refference error: Connection is not defined

is related to lack of a "Connection" object, which based on my experience with corodva 3.1.0 does not become available, even after a delay as benka suggested. This particular issue can be solved by using the constants of the navigator.connection object as below:

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';

unfortunately in my case this was only the beginning of issues with network status on android as

navigator.connection.type

would always return 0 which is Unknown connection. Both on the android emulator and a device. A workaround which works for me is to call the plugin class directly:

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;
}

this code has an ugly networkState assignment inside a function that could potentially be executed asynchronously after the checkConnection return statement, but as the native code returns PluginResult inside the execute function - this works. The networkState value returned does not match the navigator.connection. constants like:

navigator.connection.WIFI

You can see the values returned in the plugins source code here: https://github.com/apache/cordova-plugin-network-information/blob/master/src/android/NetworkManager.java


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...