使用打开关闭数据网络,触发BroadCast
1、新建BroadCastTest工程;
2、新建MyBroadCast
在OnReceive()增加一句显示代码 Toast.maketext().show();
3、在MainActivity中定义两个变量;
private IntentFilter intentFilter;启动intent,带action;
private MyBroadCast myBroadCast;
intentFilter=new IntentFilter();
intentFilter.action("android.net.conn.CONNECTION_CHANGE");
myBroadCast=new MyBroadCast();
regesterreceiver(myBroadCast,intentFilter);//注册自定义的BroadCast,带一个Action;
4、OnDestroy()
unregesterreceiver(myBroadCast);
5、在AndroidMainifest.xml中填写:
<use-permission android :name="android.permission.ACCESS_NETWORK_STATE"/>