cordova 3.0: Android: Connection is not defined

前端 未结 14 1574
情话喂你
情话喂你 2020-12-20 13:54

my first time experimenting with Apache Cordova 3.0.

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

./create ~/Docume         


        
相关标签:
14条回答
  • 2020-12-20 14:15

    Much in the same time at the end was that no load the js plugins that referenced.          cordova_plugins.js

    it may happen that your project is not in the correct folder ... but in any case you can copy directly to a folder known for.

             assets / www / plugins / org.apache.cordova.network-information

          network.js "

          Connection.js "

    and loads in your html

    0 讨论(0)
  • 2020-12-20 14:19

    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

    0 讨论(0)
  • 2020-12-20 14:20

    I had the same problem with Cordova 3.5.0: navigator.connection.type returned 0 all the time and navigator.onLine was true. It was frustrating.

    Then I found that the problem is only on my phone (with Android 2.3) and it works on emulator.

    I guess that the problem can relate with Android version too.

    0 讨论(0)
  • 2020-12-20 14:22

    If you have included the plugin and are having the same problem it could be the order in which you included the plugin.

    In order for the plugin to work I had to include the plugin after having added the platform.

    $ cordova create
    
    $ cordova platform add android 
    
    $ cordova plugin add org.apache.cordova.network-information
    
    0 讨论(0)
  • 2020-12-20 14:22

    Now days in cordova 3.5 it seems

    <access origin="*" />
    

    is the correct CORS config.xml entry.

    Note clarification on it being "ORIGIN" not "uri" and "subdomains" as it was before I believe.

    0 讨论(0)
  • 2020-12-20 14:23

    I got the same issue with Phonegap 3.0 on Android 4.2.2 Api 17.

    Tried removing and reinstalling the Connection plugin trying both commands: Cordova or Phonegap local but didn't work.

    What I have noticed in the logs is the following right after the ERROR message:

    10-11 14:31:40.360: E/Web Console(): Uncaught ReferenceError: Connection is not defined
    10-11 14:31:40.380: D/CordovaNetworkManager(): Connection Type: wifi
    

    So I was thinking that it actually looks like it was an async callback after successfully initializing Connection.type from CordovaNetworkManager() however it shouldn't be.

    So I tried the following:

    var networkState = navigator.connection.type;
    
    setTimeout(function(){
        networkState = navigator.connection.type;
        alert('networkState = '+networkState);
    
        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]);
     }, 500);
    

    I know it looks a bit stupid but works every time. What it does is first calls navigator.connection.type and then runs the whole function 500ms later giving time to CordovaNetworkManager to initialize the connection.type.

    0 讨论(0)
提交回复
热议问题