Networking navigator.connection.type不工作,即使设备已就绪*或*设备从未就绪

Networking navigator.connection.type不工作,即使设备已就绪*或*设备从未就绪,networking,cordova,Networking,Cordova,我正在尝试用Phonegap制作一个简单的应用程序,用AdobePhoneGapBuilder编译。我已经找到并使用了下面的navigator.connection.type示例,并在其中添加了另一行,以便在设备准备就绪时生成警报框。它甚至没有走那么远。我已经让它在一些点上显示了无休止的旋转循环,通过将代码从页面的头部移动到页面的主体,但这最终没有帮助。在iOs和Android设备上进行测试会得到相同的结果,config.xml确实包括:- <plugin name="NetworkSta

我正在尝试用Phonegap制作一个简单的应用程序,用AdobePhoneGapBuilder编译。我已经找到并使用了下面的navigator.connection.type示例,并在其中添加了另一行,以便在设备准备就绪时生成警报框。它甚至没有走那么远。我已经让它在一些点上显示了无休止的旋转循环,通过将代码从页面的头部移动到页面的主体,但这最终没有帮助。在iOs和Android设备上进行测试会得到相同的结果,config.xml确实包括:-

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

非常感谢您的帮助

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

//等待Cordova加载
// 
文件。添加的监听器(“deviceready”,OnDeviceraddy,false);
//Cordova已加载,现在可以安全地调用Cordova方法
//
函数ondevicerady(){
警报(“设备已准备就绪”);
检查连接();
}
函数checkConnection(){
var networkState=navigator.connection.type;
变量状态={};
状态[Connection.UNKNOWN]=“未知连接”;
状态[Connection.ETHERNET]=“以太网连接”;
状态[Connection.WIFI]=“WIFI连接”;
状态[Connection.CELL_2G]=“CELL 2G Connection”;
状态[Connection.CELL_3G]=“CELL 3G Connection”;
状态[Connection.CELL_4G]=“CELL 4G Connection”;
状态[Connection.CELL]=“单元通用连接”;
状态[Connection.NONE]=“无网络连接”;
警报('连接类型:'+状态[网络状态]);
}

您还应等待所有脚本加载完毕。将所有物品包裹在车体上,如下所示:

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();
    }
}
功能检查离线(分钟){
if(window.lastCheckTime==null){
window.lastCheckTime=0;
}
var currentTime=(新日期()).getTime();
if(currentTime<(window.lastCheckTime+minutes*60000))返回;
window.lastCheckTime=当前时间;
//ios不允许您退出应用程序,因此只需发出警告即可
//ios还要求您发出警告,否则您的应用程序将被拒绝
if(window.isIOS){
navigator.notification.alert('如果没有internet连接,此应用程序可能无法正常运行');
}否则{
navigator.notification.confirm(
'没有internet连接,此应用程序可能无法正常运行。是否继续脱机工作?',//消息
函数(button)//在按下按钮索引时调用的回调
{
如果(按钮==1){
navigator.app.exitApp();
}  
},
'警告',//标题
'退出,继续'//按钮标签
);
}
}
函数checkConnection(){
//请在此处检查连接类型代码,或仅使用navigator.onLine
如果(!navigator.onLine){
//不要生气,每5分钟确认一次
离线检查(5);
}
}
//初始phonegap deviceready处理程序
函数ondevicerady(){
log(“应用程序已启动”);
引导(文档,['AssetApp']);
if(window.isPhoneGap){
document.addEventListener(“脱机”,检查连接,错误);
检查连接();
}
};
函数onBodyLoad(){
//这些在以后的应用程序中很有用,最好尽早设置
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;
//我在chrome中调试时使用的东西
if(window.isPhoneGap){
文件。添加的监听器(“deviceready”,OnDeviceraddy,false);
}否则{
ondevicerady();
}
}

我也遇到了同样的问题,发现我必须运行“cordova build”,然后正确返回状态

当心 当我运行cordova build时,它似乎占用了我~/app/www目录中的所有内容,对app/platforms/android/assets/www目录中的所有内容进行了过度评估/

我的“安装过程”如下所示:

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
然后,我可以在app/www中进行代码更改,并在高兴时使用“cordova build”(它似乎总是将文件复制到app/platforms/android/assets/www/)对其进行“部署”

如果我使用(例如)添加另一个插件

那我得跑了

cordova build
让它工作

我希望这有帮助


(我正在使用cordova 3.3.1-0.1.2)

谢谢,这确实让我有些感动。我现在已经缩小了问题的范围。我现在得到的代码可以运行checkConnection函数(我使用了警报来验证这一点)但是,它永远不会在该函数的末尾向我提示连接是什么。进一步的测试,它会在navigator.connection.type处出现。我可以提醒此值,并且警告不会发生。进一步查看代码,我发现您使用的是navigator.onLine而不是navigator.connection.type。如果我尝试此操作,我会得到一个结果:true、 我想这会解决问题,因为我不需要知道类型,只有在我在线或不在线的情况下。但是为什么连接类型代码不起作用是个谜。navigator.online起作用了!!(navigator.connection.type有时起作用,有时不起作用)。我想知道为什么我之前没有找到有关online属性的信息。谢谢
cordova build