Android使用BroadcastReceiver监听网络连接状态的改变
作者:huaxaY 时间:2021-12-27 05:28:35
只需要实现下面2段代码即可实现对网络连接状态的监听,千万别忘了在Manifest.xml里面添加网络访问权限哦。
1、定义广播 *
NetState.java
package huaxa.it.map.net;
import huaxa.it.map.demo.MapDemo;
import com.baidu.mapapi.map.MapView;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.net.NetworkInfo.State;
import android.net.wifi.WifiInfo;
import android.util.Log;
import android.widget.Toast;
/**
* @项目名: ZhiHUIGuangDong
* @包名: huaxa.it.zhihuiguangdong
* @类名: NetState
* @创建者: 黄夏莲
* @创建时间: 2016年10月22日 ,上午1:10:16
*
* @描述: TODO
*/
public class NetState extends BroadcastReceiver
{
public int flag = 0;
private int flag1=0;
@Override
public void onReceive(Context context, Intent arg1)
{
ConnectivityManager manager = (ConnectivityManager) context
.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo activeNetworkInfo = manager.getActiveNetworkInfo();
if (activeNetworkInfo == null)
{
Toast.makeText(context, "当前无网络,请检查移动设备的网络连接", Toast.LENGTH_SHORT)
.show();
flag = 1;
flag1 = 1;
Log.i("TAG","网络未连接+flag+"+flag);
}
// activeNetworkInfo.getTypeName(); 以何种方式连线
// :cmwap/cmnet/wifi/uniwap/uninet
// activeNetworkInfo.isAvailable(); 当前网络是否可用(true)
// activeNetworkInfo.isFailover();网络有问题
else
{
if (!activeNetworkInfo.isAvailable()
|| activeNetworkInfo.isFailover())
{
Toast.makeText(context, "当前网络不可用", Toast.LENGTH_SHORT).show();
flag = 1;
flag1=2;
Log.i("TAG","当前网络不可用flag+"+flag);
}
if (flag == 1)
{
if (activeNetworkInfo.getType() == ConnectivityManager.TYPE_MOBILE)
{
Toast.makeText(context, "已连接上移动数据", Toast.LENGTH_SHORT)
.show();
} else
{
Toast.makeText(context, "已连接上WIFI数据", Toast.LENGTH_SHORT)
.show();
}
Log.i("TAG","网络ok,flag+"+flag+"....."+flag1);
}
}
}
}
2、注册Broadcast Receiver
在Activity中加入:
NetState receiver = new NetState();
IntentFilter filter = new IntentFilter();
filter.addAction(ConnectivityManager.CONNECTIVITY_ACTION);
this.registerReceiver(receiver, filter);//注册
receiver.onReceive(this, null);//接收
Broadcast Receiver知识点
每收到一次消息时便调用onReceive(Context context,Intent intent)对其处理。
Intent可以传递数据实现自定义广播事件,然后通过sendBroadcast(intent)发送出去。通过onReceive()方法处理接收的这个的数据。
String Action = "xxxxxx";//xxxxxx指的是自定义的Broadcast的action属性,如com.annyAndroid.broadcastdemo.action.USER_BROADCAST,随便定义,但是在广播 * 的intentfilter中的action属性必须和这个一致
Intent intent = new Intent();
intent.putExtra("user","huaxa");//key-Value
sendBroadcast(intent);
注意:如果在onReceive()方法中的代码执行时间超过5s的话,Android会ANR。
1、使用广播 *
public class AAAAA extends Broadcast Receiver{
@Override
onReceive(Context context,Intent intent){
//处理广播事件
......
}
}
2、注册Broadcast Receiver(2种方法)
1)、在Manifest.xml里面:
<receiver android:name=".MyReceiver">
<intent-filter>
<action android:name="xxxxxxxx"/>
<!--xxxxx广播事件的Action属性 -->
</intent-filter>
</receiver>
2、在Activity中onCreate()用Java代码实现
IntentFilter filter = new IntentFilter(xxxx);//添加事件的ACTION,如电池电量、网络连接变化、有来电、短信息等
AAAAA aAAAA = new AAAAA();
registerReceiver(aAAAA,filter);//注册
3)、在适当时取消注册Receiver,可以在程序中取消,在onDestroy()中调用如下函数即可:
if (connectionReceiver != null) {
unregisterReceiver(connectionReceiver);
}
来源:https://blog.csdn.net/u014299265/article/details/53608202
标签:Android,BroadcastReceiver,监听网络
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
java导出生成word的简单方法
2023-11-23 23:07:13
浅谈自定义校验注解ConstraintValidator
2023-07-06 03:10:53
![](https://img.aspxhome.com/file/2023/9/61529_0s.jpg)
Java泛型的使用限制实例分析
2023-05-07 20:14:52
Spring Cloud整合XXL-Job的示例代码
2021-06-09 06:01:59
![](https://img.aspxhome.com/file/2023/6/75436_0s.jpg)
Java JVM内存区域详解
2021-05-25 18:44:43
![](https://img.aspxhome.com/file/2023/3/64893_0s.jpg)
解决Jackson解析嵌套类问题(MismatchedInputException)
2023-07-26 03:09:31
![](https://img.aspxhome.com/file/2023/9/57909_0s.png)
Java并发的CAS原理与ABA问题的讲解
2023-11-25 12:17:21
![](https://img.aspxhome.com/file/2023/7/59987_0s.png)
C# 添加、修改和删除PDF书签的实例代码
2022-09-24 06:45:55
![](https://img.aspxhome.com/file/2023/4/68614_0s.png)
C# 获取指定QQ头像绘制圆形头像框GDI(Graphics)的方法
2023-03-26 14:44:41
![](https://img.aspxhome.com/file/2023/4/72484_0s.gif)
java导出大批量(百万以上)数据的excel文件
2023-11-16 13:13:22
C#多线程学习之Thread、ThreadPool、Task、Parallel四者区别
2023-08-27 05:32:14
![](https://img.aspxhome.com/file/2023/2/108772_0s.png)
在spring中手写全局异常拦 截器
2023-11-09 01:25:42
SpringBoot注册Servlet的三种方法详解
2022-06-15 15:56:29
![](https://img.aspxhome.com/file/2023/1/78921_0s.png)
Java关键字instanceof用法及实现策略
2023-09-23 06:05:38
![](https://img.aspxhome.com/file/2023/0/58560_0s.png)
Java缓存池代码实例详解
2021-07-23 07:40:39
android如何获取view在布局中的高度与宽度详解
2023-08-14 17:17:51
同时使用@LoadBalanced @RefreshScope注解负载均衡失效分析
2023-12-07 10:59:24
![](https://img.aspxhome.com/file/2023/7/62037_0s.png)
Java数据结构之复杂度篇
2022-06-18 02:32:09
Java @Deprecated注解的作用及传递性
2023-08-11 12:55:05
![](https://img.aspxhome.com/file/2023/7/58097_0s.png)
java使用@Scheduled注解执行定时任务
2021-09-16 08:37:16
![](https://img.aspxhome.com/file/2023/6/65786_0s.jpg)