Check internet connection in Android WebView (Cordova)

前端 未结 2 1095
礼貌的吻别
礼貌的吻别 2020-12-20 13:40

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 Androi

相关标签:
2条回答
  • 2020-12-20 14:14

    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( ).

    0 讨论(0)
  • 2020-12-20 14:29

    In Chrome and Safari, if the browser is not able to connect to a local area network (LAN) or a router, it is offline; all other conditions return true. So while you can assume that the browser is offline when it returns a false value, you cannot assume that a true value necessarily means that the browser can access the internet. You could be getting false positives, such as in cases where the computer is running a virtualization software that has virtual ethernet adapters that are always "connected." Therefore, if you really want to determine the online status of the browser, you should develop additional means for checking. To learn more, see the HTML5 Rocks article, http://www.html5rocks.com/en/mobile/workingoffthegrid/

    0 讨论(0)
提交回复
热议问题