Android中判断网络是否可用的代码分享
作者:junjie 时间:2022-01-03 23:59:46
package cn.hackcoder.beautyreader.broadcast;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.net.ConnectivityManager;
import android.util.Log;
import android.widget.Toast;
import cn.hackcoder.beautyreader.activity.base.BaseActivity;
import cn.hackcoder.beautyreader.utils.NetWorkUtils;
public class NetWorkStatusReceiver extends BroadcastReceiver {
public NetWorkStatusReceiver() {
}
@Override
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
if (action.equals(ConnectivityManager.CONNECTIVITY_ACTION)) {
Toast.makeText(context, "network changed", Toast.LENGTH_LONG).show();
BaseActivity.isNetWorkConnected = NetWorkUtils.getAPNType(context)>0;
}
}
}
package cn.hackcoder.beautyreader.utils;
import android.content.Context;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.telephony.TelephonyManager;
/**
* Created by hackcoder on 15-1-25.
*/
public class NetWorkUtils {
/**
* 判断是否有网络连接
* @param context
* @return
*/
public static boolean isNetworkConnected(Context context) {
if (context != null) {
ConnectivityManager mConnectivityManager = (ConnectivityManager) context
.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo mNetworkInfo = mConnectivityManager.getActiveNetworkInfo();
if (mNetworkInfo != null) {
return mNetworkInfo.isAvailable();
}
}
return false;
}
/**
* 判断WIFI网络是否可用
* @param context
* @return
*/
public static boolean isWifiConnected(Context context) {
if (context != null) {
ConnectivityManager mConnectivityManager = (ConnectivityManager) context
.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo mWiFiNetworkInfo = mConnectivityManager
.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
if (mWiFiNetworkInfo != null) {
return mWiFiNetworkInfo.isAvailable();
}
}
return false;
}
/**
* 判断MOBILE网络是否可用
* @param context
* @return
*/
public static boolean isMobileConnected(Context context) {
if (context != null) {
ConnectivityManager mConnectivityManager = (ConnectivityManager) context
.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo mMobileNetworkInfo = mConnectivityManager
.getNetworkInfo(ConnectivityManager.TYPE_MOBILE);
if (mMobileNetworkInfo != null) {
return mMobileNetworkInfo.isAvailable();
}
}
return false;
}
/**
* 获取当前网络连接的类型信息
* @param context
* @return
*/
public static int getConnectedType(Context context) {
if (context != null) {
ConnectivityManager mConnectivityManager = (ConnectivityManager) context
.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo mNetworkInfo = mConnectivityManager.getActiveNetworkInfo();
if (mNetworkInfo != null && mNetworkInfo.isAvailable()) {
return mNetworkInfo.getType();
}
}
return -1;
}
/**
* 获取当前的网络状态 :没有网络0:WIFI网络1:3G网络2:2G网络3
*
* @param context
* @return
*/
public static int getAPNType(Context context) {
int netType = 0;
ConnectivityManager connMgr = (ConnectivityManager) context
.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo networkInfo = connMgr.getActiveNetworkInfo();
if (networkInfo == null) {
return netType;
}
int nType = networkInfo.getType();
if (nType == ConnectivityManager.TYPE_WIFI) {
netType = 1;// wifi
} else if (nType == ConnectivityManager.TYPE_MOBILE) {
int nSubType = networkInfo.getSubtype();
TelephonyManager mTelephony = (TelephonyManager) context
.getSystemService(Context.TELEPHONY_SERVICE);
if (nSubType == TelephonyManager.NETWORK_TYPE_UMTS
&& !mTelephony.isNetworkRoaming()) {
netType = 2;// 3G
} else {
netType = 3;// 2G
}
}
return netType;
}
}
注册:
<receiver
android:name=".broadcast.NetWorkStatusReceiver"
android:enabled="true"
android:exported="true">
<intent-filter>
<action android:name="android.net.conn.CONNECTIVITY_CHANGE" />
</intent-filter>
</receiver>
标签:Android,判断,网络,可用
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
详解使用Spring AOP和自定义注解进行参数检查
2021-11-27 00:06:49
![](https://img.aspxhome.com/file/2023/1/70261_0s.jpg)
SpringBoot整合Mybatis实现多数据源配置与跨数据源事务实例
2023-06-29 23:47:34
Kotlin自定义View系列教程之标尺控件(选择身高、体重等)的实现
2022-06-26 00:35:40
![](https://img.aspxhome.com/file/2023/7/117077_0s.gif)
Minio与SpringBoot使用okhttp3问题解决
2021-06-25 19:17:08
![](https://img.aspxhome.com/file/2023/1/83501_0s.jpg)
Android判断设备网络连接状态及判断连接方式的方法
2023-08-29 16:41:02
解决unity3d导入模型贴图材质丢失的问题
2023-10-28 09:34:48
![](https://img.aspxhome.com/file/2023/2/126102_0s.png)
Java超详细梳理IO流的使用方法上
2023-07-21 16:31:25
![](https://img.aspxhome.com/file/2023/1/106171_0s.png)
Android编程实现图片放大缩小功能ZoomControls控件用法实例
2022-07-16 22:01:11
springboot 使用mybatis查询的示例代码
2022-03-30 03:19:09
![](https://img.aspxhome.com/file/2023/9/132179_0s.png)
用C#缩小照片上传到各种空间的具体方法
2022-03-28 02:29:18
JAVA使用hutool工具实现查询树结构数据(省市区)
2021-11-04 18:30:09
![](https://img.aspxhome.com/file/2023/0/128810_0s.png)
一文带你了解Java中的函数式编程
2022-08-02 07:32:29
Java定位问题线程解析
2023-08-09 22:04:27
![](https://img.aspxhome.com/file/2023/1/58041_0s.png)
SpringBoot全局异常处理与定制404页面的方法
2021-08-07 18:54:56
![](https://img.aspxhome.com/file/2023/9/64759_0s.png)
MybatisPlus中@TableField注解的使用详解
2021-11-01 23:05:35
![](https://img.aspxhome.com/file/2023/0/82040_0s.png)
Java 深入浅出掌握Collection单列集合Set
2023-10-07 18:49:32
![](https://img.aspxhome.com/file/2023/5/58545_0s.gif)
Java栈和基础队列的实现详解
2023-07-02 05:36:59
![](https://img.aspxhome.com/file/2023/7/61597_0s.gif)
Java毕业设计实战项目之在线服装销售商城系统的实现流程
2023-03-27 00:15:31
![](https://img.aspxhome.com/file/2023/6/71816_0s.jpg)
Android Internet应用实现获取天气预报的示例代码
2023-09-26 04:13:22
![](https://img.aspxhome.com/file/2023/4/99784_0s.jpg)
IOS开发向右滑动返回前一个页面功能(demo)
2021-09-23 04:30:11
![](https://img.aspxhome.com/file/2023/5/96015_0s.gif)