本篇文章为大家展示了怎么在Android中利用BroadcastReceiver监听网络连接状态,内容简明扼要并且容易理解,绝对能使你眼前一亮,通过这篇文章的详细介绍希望你能有所收获。
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);
}
上述内容就是怎么在Android中利用BroadcastReceiver监听网络连接状态,你们学到知识或技能了吗?如果还想学到更多技能或者丰富自己的知识储备,欢迎关注亿速云行业资讯频道。
亿速云「云服务器」,即开即用、新一代英特尔至强铂金CPU、三副本存储NVMe SSD云盘,价格低至29元/月。点击查看>>
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。