navigator.connection.type即使设备就绪也不工作*或*设备从未准备好 [英] navigator.connection.type not working even if device is ready *or* device is never ready

查看:1750
本文介绍了navigator.connection.type即使设备就绪也不工作*或*设备从未准备好的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图做一个简单的应用程序与Phonegap,用Adobe Phonegap生成器编译。我发现并使用了良好的文档示例使用navigator.connection.type,如下,并已添加另一行,以生成警报框,当设备就绪。它甚至没有那么远。我已经在一些点显示无尽的旋转圆,通过将这个代码从头到页面的主体,但这是没有帮助到底。在iOs和Android设备上测试给出了相同的结果,config.xml包括: -

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

任何帮助非常感激。

/ p>

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


推荐答案

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

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

并添加到您的body标签:

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

这篇关于navigator.connection.type即使设备就绪也不工作*或*设备从未准备好的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆