简体   繁体   English

不断检查互联网连接

[英]Check internet connection constantly

I'm developing an Android app with ionic framework and need to check the internet connection, but steadily. 我正在开发具有离子框架的Android应用,需要稳定地检查互联网连接。 For example if the user is logged in and then has no connection, I would like to display a message or any message. 例如,如果用户已登录然后没有连接,我想显示一条消息或任何一条消息。 I had thought something like 我曾经想过

<div class="bar bar-subheader" id="alerta_no_internet" ng-show="!online">
    <p>Not connected to Internet </p>
</div

What they do not do is any service or if there is something to be constantly asking about the connection. 他们不做的是提供任何服务,或者是否有需要不断询问连接的东西。

There are a few ways of going about this: 有几种解决方法:

  1. You can store an asset (maybe a 1px x 1px GIF image) on your server and constantly keep polling for this (continuous requests every x seconds). 您可以在服务器上存储资产(可能是1px x 1px GIF图像),并不断对此进行轮询(每x秒连续请求)。 Of course you wanna make sure this image is not cached. 当然,您要确保不缓存该图像。 If the request fails, you can assume that the network is not available. 如果请求失败,则可以假定网络不可用。 This is easy to implement, but is not efficient because polling sends tons of HTTP headers over the wire. 这很容易实现,但是效率不高,因为轮询会通过网络发送大量的HTTP标头。
  2. This is much more efficient - implement a WebSocket connection in your client and server and open a socket upon opening the app. 这效率更高-在客户端和服务器中实现WebSocket连接,并在打开应用程序时打开套接字。 If the socket drops, you can display the Connectivity Lost message. 如果套接字断开,则可以显示“ Connectivity Lost消息。 This doesn't require sending of HTTP requests repeatedly. 这不需要重复发送HTTP请求。 This is much more efficient, but more work will be required to implement this. 这样效率更高得多,但是要实现此目标还需要做更多的工作。

You can write services for either of these in Ionic. 您可以在Ionic中为其中任何一个编写服务。

At it's simplest, option #1 can be: 最简单的选择#1可以是:

setInterval(ajaxCall, 3000); //3000 MS == 3 sec

function ajaxCall() {
  $.ajax('/testConnectivityUrl').then(function(response) {
    console.log("Still connected");
  }, function(err) {
    console.log("Lost connection");
  }
}

I've used jQuery, but you can do this with vanilla JS or Angular or Ionic services or factories. 我使用过jQuery,但是您可以使用香草JS或Angular或Ionic服务或工厂来实现。

I don't know much about the ionic framework, but the ConnectivityManager in the Android framework does emit a broadcast on connectivity changes . 我对ionic框架了解不多,但是Android框架中的ConnectivityManager确实会发出有关连接更改广播 You can implement a BroadcastReceiver to listen for these changes: 您可以实现BroadcastReceiver来侦听这些更改:

<receiver android:name=".ConnectivityReceiver">
    <intent-filter>
        <action android:name="android.net.conn.CONNECTIVITY_CHANGE"/>
    </intent-filter>
</receiver>

Then access the ConnectivityManager to monitor the state of the network : 然后访问ConnectivityManager监视网络状态

public class ConnectivityReceiver extends BroadcastReceiver {

    @Override
    public void onReceive(Context context, Intent intent) {
        ConnectivityManager manager =
                (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);

        NetworkInfo info = manager.getActiveNetworkInfo();

        // TODO: monitor state using NetworkInfo

    }

}

Make sure you declare the internet and network state permissions in the Manifest as well: 确保同时在Manifest声明Internet和网络状态权限:

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

Be aware though that connectivity changes can be frequent, therefore monitoring them can be costly. 请注意,尽管连接更改可能很频繁,所以监视它们的成本可能很高。 According to best practices you should toggle this receiver on only when it is needed: 根据最佳实践,您仅应在需要时打开此接收器

It's generally sufficient to simply check for Internet connectivity before beginning an update and, should there be none, suspend further updates until connectivity is restored. 在开始更新之前,仅检查Internet连接通常就足够了;如果没有连接,则暂停进一步的更新,直到恢复连接为止。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM