在Android WebView(Cordova)检查互联网连接

编程入门 行业动态 更新时间:2024-10-27 02:23:31
本文介绍了在Android WebView(Cordova)检查互联网连接的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我知道在Stackoverflow有很多问题和答案,我读了很多,但没有一个工作。

我在title Android WebView因为它是最重要的目标,但我想这在其他设备也工作。我在使用Android 4.4.2版本的SM-G355M上安装了Intel XDK的应用程式以及安装在搭载iOS的iPhone 5C上的Safari上测试了以下程式码9.3.4 ;

我在两种情况下获得的值都是相同的, true

我试过:

  • code> document.addEventListener(online,... 不触发
  • ajax 不起作用,反正不会影响服务器。
  • ::

    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:

  • navigator.onLine, it gives always the same value.
  • document.addEventListener("online", ... doesn't trigger
  • ajax doesn't work, anyway doesn't affect to the server?
  • Code: nanilab/stackoverflow/webview-internet-connection.php

    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

    youtu.be/wHJHG5dP_eM

    What I am doing wrong?

    解决方案

    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.cordovaworkinformation.NetworkManager" /> </feature>

    Quick Guide Cordova installation goto web page for installation instructions

    cordova.apache/docs/en/latest/guide/cli/

    goto web page and download nodejs for your system

    nodejs/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'm 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( ). Where are all the Cordova programmers?, I only heard of Cordova last week:

    更多推荐

    在Android WebView(Cordova)检查互联网连接

    本文发布于:2023-11-27 16:59:46,感谢您对本站的认可!
    版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
    本文标签:互联网   Android   WebView   Cordova

    发布评论

    评论列表 (有 0 条评论)
    草根站长

    >www.elefans.com

    编程频道|电子爱好者 - 技术资讯及电子产品介绍!