检查网络和Internet连接 - Android [英] Check Network and Internet Connection - Android

查看:106
本文介绍了检查网络和Internet连接 - Android的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道下面的方法是否检查我是否都连接到网络,并且实际上也可以连接到互联网。

I was wondering if the method below check that I am both connected to a network, and can actually connected to the internet as well.

不仅仅是连接到网络不允许我访问互联网的网络?

Not just connected to a network that won't let me access the internet ?

public boolean isNetworkAvailable() {
    ConnectivityManager manager = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
    NetworkInfo networkInfo = manager.getActiveNetworkInfo();

    boolean isAvailable = false;
    if (networkInfo != null && networkInfo.isConnected()) {
        isAvailable = true;
    }
    return isAvailable;
}

我认为,确实如此,但我不是百分百确定。

I think, it does but I am not 100% sure.

谢谢

推荐答案

将此帖子上接受的答案与您的代码进行比较,你在做什么应该工作。随意比较代码。最安全的做法是从飞机模式运行一些测试,关闭WiFi,以及从远离WiFi的位置进行测试。祝你好运。

On comparing the accepted answer on this post to your code, what you are doing should work. Feel free to compare code. The safest thing to do would be to run a few tests from airplane mode, with the WiFi turned off, and from a location away from WiFi just to be sure. Good luck.

Android - 如果没有连接,则以编程方式检查互联网连接并显示对话框

这篇关于检查网络和Internet连接 - Android的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆