Android编程判断是否连接网络的方法【WiFi及3G判断】
作者:Jacob-wj 时间:2022-01-28 02:31:09
本文实例讲述了Android编程判断是否连接网络的方法。分享给大家供大家参考,具体如下:
判断wifi网络是否链接:
public static boolean isWiFiActive(Context inContext) {
WifiManager mWifiManager = (WifiManager) inContext
.getSystemService(Context.WIFI_SERVICE);
WifiInfo wifiInfo = mWifiManager.getConnectionInfo();
int ipAddress = wifiInfo == null ? 0 : wifiInfo.getIpAddress();
if (mWifiManager.isWifiEnabled() && ipAddress != 0) {
System.out.println("**** WIFI is on");
return true;
} else {
System.out.println("**** WIFI is off");
return false;
}
}
判断3G网络是否链接:
public static boolean isNetworkAvailable( Context context) {
ConnectivityManager connectivity = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
if (connectivity == null) {
System.out.println("**** newwork is off");
return false;
} else {
NetworkInfo info = connectivity.getActiveNetworkInfo();
if(info == null){
System.out.println("**** newwork is off");
return false;
}else{
if(info.isAvailable()){
System.out.println("**** newwork is on");
return true;
}
}
}
System.out.println("**** newwork is off");
return false;
}
相关权限:
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
PS:关于AndroidManifest.xml文件相关属性功能可参考本站在线工具:
Android Manifest功能与权限描述大全:
http://tools.jb51.net/table/AndroidManifest
希望本文所述对大家Android程序设计有所帮助。
标签:Android,网络,WiFi,3G
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
如何利用C#正则表达式判断是否是有效的文件及文件夹路径
2022-03-06 23:34:45
Java 类与对象超基础讲解
2023-06-12 00:03:22
![](https://img.aspxhome.com/file/2023/8/70938_0s.png)
Java开发环境jdk 1.8安装配置方法(Win7 64位系统/windows server 2008)
2022-05-11 20:00:58
![](https://img.aspxhome.com/file/2023/0/60310_0s.jpg)
C#数据结构与算法揭秘三 链表
2022-04-17 03:22:38
![](https://img.aspxhome.com/file/2023/4/88314_0s.png)
Springboot动态切换数据源的具体实现与原理分析
2022-11-24 13:45:26
![](https://img.aspxhome.com/file/2023/8/70918_0s.png)
C#常用GDI+文字操作汇总
2022-10-24 00:51:12
C# 批处理调用方法
2021-06-18 17:14:19
Java 超详细讲解类的定义方式和对象的实例化
2021-09-11 17:38:29
![](https://img.aspxhome.com/file/2023/9/70939_0s.png)
Java Set集合及其子类HashSet与LinkedHashSet详解
2023-11-26 11:39:35
![](https://img.aspxhome.com/file/2023/0/59510_0s.png)
Android使用Websocket实现聊天室
2023-07-07 13:01:59
![](https://img.aspxhome.com/file/2023/6/60386_0s.png)
C#中dynamic关键字的正确用法(推荐)
2023-11-12 09:20:22
使用JSONObject生成和解析json的方法
2022-06-15 11:13:15
基于rocketmq的有序消费模式和并发消费模式的区别说明
2021-10-29 08:41:02
![](https://img.aspxhome.com/file/2023/0/80310_0s.png)
详细聊聊如何在C#循环中捕获局部变量
2021-10-30 17:05:13
Java读取、写入文件如何解决乱码问题
2023-08-26 11:16:00
C#移除所有事件绑定的方法
2023-07-25 11:00:52
通过Java实现在Word中创建可填充表单
2023-08-05 21:11:40
![](https://img.aspxhome.com/file/2023/4/58034_0s.jpg)
java使用influxDB数据库的详细代码
2023-03-04 05:22:27
SpringBoot上传图片到指定位置并返回URL的实现
2023-08-13 14:02:52
![](https://img.aspxhome.com/file/2023/4/88064_0s.jpg)
详解C++ STL模拟实现forward_list
2023-06-21 02:36:04
![](https://img.aspxhome.com/file/2023/0/60100_0s.jpg)