检查android上的互联网连接

Checking internet connection on android(检查android上的互联网连接)
本文介绍了检查android上的互联网连接的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下代码用于检查我的应用程序上的互联网连接 wifi/EDGE/GPRS/3G.

I have the following code for checking internet connection wifi/EDGE/GPRS/3G on my application.

代码是

public static boolean checkConn(Context ctx) {
    ConnectivityManager conMgr = (ConnectivityManager) ctx
            .getSystemService(Context.CONNECTIVITY_SERVICE);
    if (conMgr.getNetworkInfo(0).getState() == NetworkInfo.State.CONNECTED
        || conMgr.getNetworkInfo(1).getState() == NetworkInfo.State.CONNECTING) {
        return true;
    } else if (conMgr.getNetworkInfo(0).getState()==NetworkInfo.State.DISCONNECTED
        || conMgr.getNetworkInfo(1).getState() == NetworkInfo.State.DISCONNECTED){
        return false;
    }
    return false;
}

我这样称呼它:

if (CheckInternet.checkConn(introPage.this) == true) {
    Intent toMainPage = new Intent(introPage.this, mainPage.class);
    System.gc();
    startActivity(toMainPage);
} else if (CheckInternet.checkConn(getApplicationContext()) == false) {
    Toast.makeText(getApplicationContext(),
        "Sorry, No internet connectivity found", Toast.LENGTH_SHORT)
            .show();
}

但是我有一个问题,如果我连接到 wifi,并且我打开应用程序,它工作正常,但如果我关闭应用程序并关闭 wifi 并重新打开应用程序,它不会通过没有连接"的错误,我需要关闭我的设备然后打开它,同样的情况是如果wifi关闭,我打开应用程序,它会抛出无连接"的错误,如果我打开它,它仍然会抛出相同的无连接"错误,直到我关闭并打开设备.

But I am having an issue, which is that if I am connected to wifi, and I open the application, it works fine, but if I close application and turn off wifi and re-open application, it doesn't through the error of "no connection" , I need to turn off my device and then turn it on, and same case is if wifi is off, and I open application, it throws error of "no connection", and if I turn it on, still it throws the same error of "no connection", until unless I turn off and on device.

推荐答案

有时活动连接不是列表中的第一个,或者处于非活动状态或处于错误状态.我会这样做:

Sometimes the active connection is not first in the list, or is inactive or in an error state. This is how I would do it:

  NetworkInfo i = conMgr.getActiveNetworkInfo();
  if (i == null)
    return false;
  if (!i.isConnected())
    return false;
  if (!i.isAvailable())
    return false;
  return true;

不要忘记在应用程序清单中添加此权限:

Don't forget to add this permission in the application manifest:

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

这对你有帮助吗?

伊曼纽尔

这篇关于检查android上的互联网连接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

How to target newer versions in .gitlab-ci.yml using auto devops (java 11 instead of 8 and Android 31 instead of 29)(如何在.gitlab-ci.yml中使用自动开发工具(Java 11而不是8,Android 31而不是29)瞄准较新的版本)
Android + coreLibraryDesugaring: which Java 11 APIs can I expect to work?(Android+core LibraryDesugering:我可以期待哪些Java 11API能够工作?)
How to render something in an if statement React Native(如何在If语句中呈现某些内容Reaction Native)
How can I sync two flatList scroll position in react native(如何在本机Reaction中同步两个平面列表滚动位置)
Using Firebase Firestore in offline only mode(在仅脱机模式下使用Firebase FiRestore)
Crash on Google Play Pre-Launch Report: java.lang.NoSuchMethodError(Google Play发布前崩溃报告:java.lang.NoSuchMethodError)