2

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>
4

2 回答 2

6

您还应该等到所有脚本都加载完毕。将所有内容包装在 onBodyLoad 中,如下所示:

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

并添加到您的身体标签:

<body onload="onBodyLoad()"> 

以及我的其余代码以供其他参考:

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();
    }
}
于 2013-07-19T19:48:53.243 回答
1

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 )

于 2014-01-29T11:54:10.453 回答