Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
454 views
in Technique[技术] by (71.8m points)

javascript - Check internet connection in Android WebView (Cordova)

I know there is a lot of questions and answers about this in Stackoverflow, I read a lot of them, but none of them work.

I clarified in the title Android WebView because it is the most important target, but I would like this works in other devices too. I tested the following code on a app built with Intel XDK installed on a SM-G355M with Android 4.4.2 and on Safari installed on an iPhone 5C with iOS 9.3.4;

All I get in both cases is the same value, true (sometimes I get false even I have an internet connection).

I tried:

  1. navigator.onLine, it gives always the same value.
  2. document.addEventListener("online", ... doesn't trigger
  3. ajax doesn't work, anyway doesn't affect to the server?

Code: https://nanilab.com/stackoverflow/webview-internet-connection.php(This link is now broken)


Option 1:

function option1(){
    var isOffline = 'onLine' in navigator && !navigator.onLine,
        text = isOffline == true ? ' without connection ' : ' connected ';

    $('.option-one span').text(text);
    $('.option-one i').text('checked').hide().fadeIn(200);

    setTimeout(function(){
        option1();
    }, 1000);
}

Option 2:

window.addEventListener("offline", function(){ $('.option-two span').text(' without connection'); }, false);
window.addEventListener("online", function(){ $('.option-two span').text(' connected'); }, false);

Option 3:

function option3(){
    $.ajax({
        url: '/stackoverflow/blank.php',
        success: function(data){
            print(' connected ');
        },
        error: function(jqXHR, textStatus, error) {
            print(' without connection ');
        }
    }); 

    function print(text){
        $('.option-three span').text(text);
        $('.option-three i').text('checked').hide().fadeIn(200);

        setTimeout(function(){
            option3();
        }, 2000);
    }
}

app built with Intel XDK installed on a SM-G355M with Android 4.4.2

https://youtu.be/wHJHG5dP_eM


What I am doing wrong?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Apache Cordova (was called PhoneGap) is an open-source mobile development framework. It allows you to use standard web technologies - HTML5, CSS3, and JavaScript for cross-platform development. Applications execute within wrappers targeted to each platform, and rely on standards-compliant API bindings to access each device's capabilities such as sensors, data, network status, etc. document reference cordova

In your problem (Option 1):

navigator.onLine 

...is not working because (on android) it is broken {the "raw" version, Cordova enabled webview is different}(as you have found out), you have to built your WebView App with the Cordova Framework. Cordova was developed EXACTLY to solve this problem. The GAP in PhoneGap is the gap between the "virtual machine", "sandbox" and access to the hardware, AND it's cross-platform.

Android Permissions: app/AndroidManifest.xml

<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.READ_PHONE_STATE" />

Cordova Permissions: app/res/xml/config.xml

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

Quick Guide Cordova installation

goto web page for installation instructions

https://cordova.apache.org/docs/en/latest/guide/cli/

goto web page and download nodejs for your system

https://nodejs.org/en/download/

example file

node-v4.5.0-x86.msi

run (install it)

success.

on Windows:

C:>npm install -g cordova

And away you go!

I have built your code into cordova, I'm getting there (hopefully, hard problem), here's some image's of what I have so far [not in WebView exactly yet, {see the navigator.userAgent output in the second image}] (notice the event listener is working ;O), but not good enough:o( ).

demo image offline demo image online


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...