android 判断网络是否可用与连接的网络是否能上网
作者:security_yn 时间:2021-10-08 02:04:34
网络状态获取
上传与下载都需要先查看当前手机的网络状态,需要获取ConnectionManager
/**
* 判断当前是否有网络连接,但是如果该连接的网络无法上网,也会返回true
* @param mContext
* @return
*/
public static boolean isNetConnection(Context mContext) {
if (mContext!=null){
ConnectivityManager connectivityManager = (ConnectivityManager) mContext.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo networkInfo = connectivityManager.getActiveNetworkInfo();
boolean connected = networkInfo.isConnected();
if (networkInfo!=null&&connected){
if (networkInfo.getState()== NetworkInfo.State.CONNECTED){
return true;
}else{
return false;
}
}
}
return false;
}
网络能否正常上网
当有网络连接时,如果想要检测当前连接的网络能否上网,需要能否打开网址来做判断
/**
* 在子线程里开启该方法,可检测当前网络是否能打开网页
* true是可以上网,false是不能上网
*
*/
public static boolean isOnline(){
URL url;
try {
url = new URL("https://www.baidu.com");
InputStream stream = url.openStream();
return true;
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return false;
}
来源:http://blog.csdn.net/qq_38357358/article/details/79208325
标签:android,判断,网络,上网
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
Java一维数组和二维数组元素默认初始化值的判断方式
2022-03-08 21:17:49
![](https://img.aspxhome.com/file/2023/4/125234_0s.png)
Servlet的5种方式实现表单提交(注册小功能),后台获取表单数据实例
2022-08-19 20:58:06
![](https://img.aspxhome.com/file/2023/3/76323_0s.png)
java实现文件压缩成zip的工具类
2022-09-25 09:13:17
C# 通过ServiceStack 操作Redis
2023-12-13 06:18:01
![](https://img.aspxhome.com/file/2023/2/80022_0s.png)
java使用jaxb操作xml示例
2023-11-05 15:02:18
详解Spring Boot加载properties和yml配置文件
2023-11-24 07:14:09
c# 实现文件上传下载功能的实例代码
2021-12-10 15:00:30
C#调用Windows的API实现窗体动画
2022-09-09 13:05:22
![](https://img.aspxhome.com/file/2023/4/113764_0s.png)
C# 循环判断会进来几次的实现代码
2021-12-27 15:10:51
![](https://img.aspxhome.com/file/2023/9/106409_0s.jpg)
C#代码设置开机启动示例
2021-12-16 17:53:07
![](https://img.aspxhome.com/file/2023/1/108621_0s.jpg)
Java数据结构之线索化二叉树的实现
2022-03-16 15:25:34
![](https://img.aspxhome.com/file/2023/1/75021_0s.png)
基于@RestControllerAdvice与@ControllerAdvice的区别说明
2022-06-24 21:00:36
![](https://img.aspxhome.com/file/2023/6/96266_0s.png)
android获取ibeacon列表的方法
2023-01-18 00:57:54
Java编程Nashorn实例代码
2022-07-30 05:28:21
详解java倒计时三种简单实现方式
2023-11-11 16:47:38
![](https://img.aspxhome.com/file/2023/8/62508_0s.png)
C语言结构体版学生成绩管理系统
2021-11-05 03:29:41
Java分布式服务框架Dubbo介绍
2022-09-16 01:27:53
![](https://img.aspxhome.com/file/2023/1/68701_0s.png)
SpringMVC通过RESTful结构实现页面数据交互
2022-01-23 08:57:24
![](https://img.aspxhome.com/file/2023/6/80916_0s.png)
C#使用Chart绘制曲线
2023-03-12 19:08:56
Java中的ArrayList容量及扩容方式
2023-10-17 17:24:40