navigator.connection.type not working even if devi

2019-02-19 13:35发布

I'm trying to make a simple app with Phonegap, compiled with Adobe Phonegap builder. I've found and used the well documented example for using navigator.connection.type which is below, and to which I've added another line, to generate an alert box when the device is ready. It doesn't even get that far. I've had it at some points show that endless spinning circle, by moving this code from the head to the body of the page, but that is no help in the end. Testing on iOs and Android devices gives the same result, and the config.xml does include:-

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

Any help greatly appreciated.

// Wait for Cordova to load
// 
document.addEventListener("deviceready", onDeviceReady, false);

// Cordova is loaded and it is now safe to make calls Cordova methods
//
function onDeviceReady() {
    alert('Device is ready');
    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('Connection type: ' + states[networkState]);
}

</script>

2条回答
贪生不怕死
2楼-- · 2019-02-19 14:00

I had the same issue and found I had to run "cordova build" and then the status was returned correctly.

BEWARE When I run cordova build, it appears to take everything in my ~/app/www directory and overried everything in app/platforms/android/assets/www/

My "install process" is as follows:

cordova create app com.app "App"
cd app
cordova platform add android
cordova plugin add org.apache.cordova.network-information
cordova plugin add org.apache.cordova.camera
cordova plugin add org.apache.cordova.geolocation
cordova build

I can then do code changes in app/www and when happy, 'deploy' it using 'cordova build' (which seems to always copy the files to app/platforms/android/assets/www/.

If I add another plugin using: (for example)

cordova plugin add org.apache.cordova.file

then I need to run

cordova build

to have it work.

I hope this helps

(I am using cordova 3.3.1-0.1.2 )

查看更多
劳资没心,怎么记你
3楼-- · 2019-02-19 14:21

You should also wait until all your scripts are loaded. Wrap everything in a onBodyLoad like so:

function onBodyLoad() {
    // these are useful later in the app, might as well set early
    window.isRipple = (window.tinyHippos != null);
    window.isPhoneGap = /^file:\/{3}[^\/]/i.test(window.location.href);
    window.isIOS = !window.isRipple && navigator.userAgent.match(/(ios|iphone|ipod|ipad)/gi) != null;
    window.isAndroid = !window.isRipple && navigator.userAgent.match(/(android)/gi) != null;

    // stuff I use for debugging in chrome
    if (window.isPhoneGap) {
        document.addEventListener("deviceready", onDeviceReady, false);
    } else {
        onDeviceReady();
    }
}

And add to your body tag:

<body onload="onBodyLoad()"> 

And the rest of my code for additional references:

function checkOffLine(minutes) {
    if (window.lastCheckTime == null) {
        window.lastCheckTime = 0;
    }

    var currentTime = (new Date()).getTime();
    if (currentTime < (window.lastCheckTime + minutes * 60000)) return;
    window.lastCheckTime = currentTime;

    // ios does not allow you to exit the application so just warn
    // ios also require you to warn or your app get rejected
    if (window.isIOS) {
        navigator.notification.alert('This application may not function properly without an internet connection.');
    } else {
        navigator.notification.confirm(
            'This application may not function properly without an internet connection.  Continue working offline?', // message
            function(button)      // callback to invoke with index of button pressed
            {
                if (button == 1) {
                    navigator.app.exitApp();
                }  
            },
                'Warning', // title
                'Exit,Continue'                     // buttonLabels
            );
        }
}

function checkConnection() {
    // your check connection type code here or just use navigator.onLine
    if (!navigator.onLine) {
        // don't be annoying, only confirm for once every every 5 minutes
        checkOffLine(5);
    }
}

// initial phonegap deviceready handler
function onDeviceReady() {
    console.log('Application started');
    angular.bootstrap(document, ['assetsApp']);
    if (window.isPhoneGap) {
        document.addEventListener("offline", checkConnection, false);
        checkConnection();
    }
};

function onBodyLoad() {
    // these are useful later in the app, might as well set early
    window.isRipple = (window.tinyHippos != null);
    window.isPhoneGap = /^file:\/{3}[^\/]/i.test(window.location.href);
    window.isIOS = !window.isRipple && navigator.userAgent.match(/(ios|iphone|ipod|ipad)/gi) != null;
    window.isAndroid = !window.isRipple && navigator.userAgent.match(/(android)/gi) != null;

    // stuff I use for debugging in chrome
    if (window.isPhoneGap) {
        document.addEventListener("deviceready", onDeviceReady, false);
    } else {
        onDeviceReady();
    }
}
查看更多
登录 后发表回答