接入网络的可用性状态的android [英] Access network availability state android

查看:205
本文介绍了接入网络的可用性状态的android的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我发展,我想访问是否有互联网连接或不是一个应用程序。我可以通过访问网络状态

I am developing an application where i want to access whether there is internet connectivity or not. I can access the network state by using

private Boolean isNetworkAvailable() {
    ConnectivityManager connectivityManager 
    = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
    NetworkInfo activeNetworkInfo = connectivityManager.getActiveNetworkInfo();
    return activeNetworkInfo != null && activeNetworkInfo.isConnected();
}

但是,这仅返回移动是否被连接到互联网或没有。我想知道是否有连接互联网后的可用性。像,可能是服务器已关闭或网络不可用。请让我知道如何做到这一点!

But this just returns whether the mobile is connected to internet or not. I want to know if there is internet availability after connecting. Like, may be the server is down or internet not available. Please let me know how to accomplish this!

推荐答案

试试这个样子,

URL url = new URL("Your URL");
URLConnection conexion = url.openConnection();
conexion.setConnectTimeout(10000); // Don't forget to put a time limit
conexion.connect();

超时(10秒上面提到的例子)后,它将通过超时异常。
所以,你可以用它来检查上网是否可用。

After timeout (10 secs for above mentioned example), It will through time out exception. So You can use it to check whether Internet access is available or not.

这篇关于接入网络的可用性状态的android的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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