Android开发之使用广播监听网络状态变化
我们经常需要判断网络状态的变化,如有无网络,所以需要监听网络状态的变化,比如网络断开,网络连接给予友好提示。如何监听网络状态的变化呢,最近工作中需要用到这个,于是就用广播机制来实现了网络状态的监听。
使用广播机制来监听网络状态主要涉及到以下几点:
1.需要自己编写个网络广播接收器,该接收器继承BroadcastReceiver类,用来接收系统发出的广播。比如我自己写了个NetStatusReceiver广播接收器。代码如下:
package com.log.system; import android.content.BroadcastReceiver; import android.content.Context; import android.content.Intent; import android.net.ConnectivityManager; import android.net.NetworkInfo; import android.widget.Toast; public class NetStatusReceiver extends BroadcastReceiver{ public static final int NETSTATUS_INAVAILABLE = 0; public static final int NETSTATUS_WIFI = 1; public static final int NETSTATUS_MOBILE = 2; public static int netStatus = 0; public static boolean updateSuccess = false; private INetStatusListener mINetStatusListener; public void onReceive(Context context, Intent intent) { ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE); NetworkInfo mobileNetInfo = cm.getNetworkInfo(ConnectivityManager.TYPE_MOBILE); NetworkInfo wifiNetInfo = cm.getNetworkInfo(ConnectivityManager.TYPE_WIFI); NetworkInfo allNetInfo = cm.getActiveNetworkInfo(); if (allNetInfo == null) { if (mobileNetInfo != null && (mobileNetInfo.isConnected() || mobileNetInfo.isConnectedOrConnecting())) { netStatus = NETSTATUS_MOBILE; } else if (wifiNetInfo != null && wifiNetInfo.isConnected() || wifiNetInfo.isConnectedOrConnecting()) { netStatus = NETSTATUS_WIFI; } else { netStatus = NETSTATUS_INAVAILABLE; } } else { if (allNetInfo.isConnected() || allNetInfo.isConnectedOrConnecting()) { if (mobileNetInfo.isConnected() || mobileNetInfo.isConnectedOrConnecting()) { netStatus = NETSTATUS_MOBILE; } else { netStatus = NETSTATUS_WIFI; } } else { netStatus = NETSTATUS_INAVAILABLE; } } if(mINetStatusListener != null){ mINetStatusListener.getNetState(netStatus); } // if (netStatus == NETSTATUS_INAVAILABLE) { // Toast.makeText(context, "网络未连接",Toast.LENGTH_SHORT).show(); // } else if (netStatus == NETSTATUS_MOBILE) { // Toast.makeText(context, "网络处于移动网络",Toast.LENGTH_SHORT).show(); // } else { // Toast.makeText(context, "网络处于Wifi网络",Toast.LENGTH_SHORT).show(); // } } public void setNetStateListener(INetStatusListener listener){ mINetStatusListener = listener; } public interface INetStatusListener{ public void getNetState(int state); } }
2.注册广播接收器
首先new一个广播接收器,然后对该接收器进行注册,也就是注册广播,然后并设置监听器。注册广播和设置监听的实现如下:
netReceiver = new NetStatusReceiver();
setRegisterReceiver(netReceiver);
setNetListener();
对应的具体方法实现代码如下:
public void setRegisterReceiver(NetStatusReceiver netReceiver){ IntentFilter filter = new IntentFilter(); filter.addAction(ConnectivityManager.CONNECTIVITY_ACTION); registerReceiver(netReceiver, filter); } public void setNetListener(){ netReceiver.setNetStateListener(new INetStatusListener() { @Override public void getNetState(int state) { // TODO Auto-generated method stub if (state == NetStatusReceiver.NETSTATUS_INAVAILABLE) { Toast.makeText(getBaseContext(), "网络未连接", Toast.LENGTH_SHORT).show(); } else { Toast.makeText(getBaseContext(), "网络已经连接", Toast.LENGTH_SHORT).show(); } } }); }
3.为了要获取网络的状态,需要在manifest文件中设置相关权限,即配置:
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />