Why doesn't navigator.onLine work on Android?

1,996 views
Skip to first unread message

Steve Husting

unread,
Jun 18, 2014, 2:23:17 PM6/18/14
to phon...@googlegroups.com
I'm using the following code to detect screen size, then if it's large, to do a connection check to let the user know, if the connection is broken, that they won't be able to download wallpaper. 

This script works fine with the iPad. Testing it with Android (Nexus 7, Android V4.3, sdkv16) the screen merely goes dark after the splashcreen. None of the alerts fire.

<!DOCTYPE html>
<html lang="en">
<head>
<meta content="text/html; charset=utf-8" http-equiv="Content-Type">
<meta content="minimum-scale=1.0, width=device-width, maximum-scale=1.0, user-scalable=no" name="viewport">
<script type="text/javascript" charset="utf-8" src="cordova.js"></script>
<script type="text/javascript" charset="utf-8">
        document.addEventListener("deviceready", onDeviceReady, false);
function onDeviceReady() {
        alert("onDeviceReady");

if (screen.width > 640) { 
        checkConnection();
        
        function checkConnection() {
        alert("checkConnection");
    if (!navigator.onLine) { 
navigator.notification.alert('No Internet connection available. You cannot download wallpaper at this time.',
    alertDismissed, // callback
    'No Connection', // title
    'Done' // buttonName
);
}
}
else { 
window.location.href = "iphone/index.html";
}

function alertDismissed() {
    window.location = "ipad/index.html";
}

</script>

<style type="text/css">
body { background-color: #333333; }
</style>
</head>
<body>

</body>
</html>

Plugin installed: 

org.apache.cordova.network-information 0.2.9 "Network Information"

AndroidManifest:
    <uses-sdk android:minSdkVersion="10" android:targetSdkVersion="16" />
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />

config.xml:
<feature name="NetworkStatus">
    <param name="android-package" value="org.apache.cordova.networkinformation.NetworkManager" />
</feature>

My development environment:

Mac OS X 10.9.3 on latest Mac Mini

Cordova CLI 3.5.0 (cordova, not phonegap)

for Android 4.3 targetSdk v17

ADT v22.0.1 with Eclipse platform (not using Eclipse at this time)

Testing in device: Nexus 7 2012 with 4.3 Jelly Bean ("cordova build")

Not for PG Build; using JQuery after the first page

Not single-page architecture

Kerri Shotts

unread,
Jun 18, 2014, 5:55:10 PM6/18/14
to phon...@googlegroups.com
Is that supported on Android? If you debug remotely, can you see if the "onLine" property exists on "navigator"?

Steve Husting

unread,
Jun 20, 2014, 10:51:08 AM6/20/14
to phon...@googlegroups.com
The Network Information plugin was the first thing I tried. When I got no response I began trying other options. I'll go back to it, but go down to sdk v17 and try again.

Kerri Shotts

unread,
Jun 21, 2014, 5:03:27 PM6/21/14
to phon...@googlegroups.com
If you use remote debugging on the device(s) giving you trouble, is "navigator.onLine" a valid property?

Steve Husting

unread,
Jun 23, 2014, 11:54:45 AM6/23/14
to phon...@googlegroups.com
I'm not using remote debugging on Android.

Steve Husting

unread,
Jun 23, 2014, 2:52:43 PM6/23/14
to phon...@googlegroups.com
OK, here's the Eclipse LogCat output from remote debugging on my device when I started the app again after turning off wifi.

Thanks,
Steve H
logcat-for-crash-with-no-wifi.jpg

Kerri Shotts

unread,
Jun 23, 2014, 3:59:47 PM6/23/14
to phon...@googlegroups.com
Well that looks... nasty. What version android?

Kerri Shotts

unread,
Jun 23, 2014, 4:01:40 PM6/23/14
to phon...@googlegroups.com
Any particular reason? If the Android version is < 4.4, you might want to look at jsHybugger -- it's not free, but it lets you use Chrome debugging on older Android builds and generally will pay for itself very quickly (if you avoid even one bruise from your head encountering the desk, it's a good buy).

Or Weinre would work to test this as well. 

Steve Husting

unread,
Jun 23, 2014, 5:46:44 PM6/23/14
to phon...@googlegroups.com
Testing in device: Nexus 7 2012 with v4.3 Jelly Bean ("cordova build")

Steve Husting

unread,
Jun 23, 2014, 5:58:24 PM6/23/14
to phon...@googlegroups.com
I don't do remote debugging because my apps are extremely simple. Never needed to do it before. So weinre is good for ios and android. 
Reply all
Reply to author
Forward
0 new messages