navigator.onLine is not working in my mobile.How to check internet is online.offline??? In Phonegap

≡放荡痞女 提交于 2019-12-01 03:56:41

问题


I am using phonegap for my app. My app is basicaly for RSS_FEED from one website. but my requirement is when internet is not there application should alert-offline.

when application run on online all data stored into database. and when internet is not there that time data fetched from DB so my problem is only that application shows me online every time while using of navigator.onLine.

if(navigator.onLine) {
    alert("check online");
    loadScript("http://www.google.com/jsapi",msgOnFn)
    loadScript("js/googleapi.js", msgOnFn);
} else {
    loadScript("js/db.js",msgOffFn);
}

I tried THIS ALSO In this it shows none or wifi everytime. I am not getting why this happening.

in web browser,It is working fine. :)


回答1:


please follow the phonegap documentation.

You could also change the given function to return true or false:

function checkConnection() {
    var networkState = navigator.connection.type;

    var states = {};
    states[Connection.UNKNOWN]  = false;
    states[Connection.ETHERNET] = true;
    states[Connection.WIFI]     = true;
    states[Connection.CELL_2G]  = true;
    states[Connection.CELL_3G]  = true;
    states[Connection.CELL_4G]  = true;
    states[Connection.CELL]     = true;
    states[Connection.NONE]     = false;
    if (states[networkState]) {
        return true;
    } else {
        return false;
    }
}


来源:https://stackoverflow.com/questions/22448591/navigator-online-is-not-working-in-my-mobile-how-to-check-internet-is-online-off

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!