只需要实现下面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-ValuesendBroadcast(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); }
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持武林网。
新闻热点
疑难解答