ThinkAndroid框架 网络状态监听原理
ThinkAndroid框架 网络状态监听原理,有需要的朋友可以参考下。
现在用到网络监听的地方越来越广泛,这里提供下ThinkAndroid里的实现方法,还是根据功能原理倒着给出代码:
1、网络状态会发出广播,监听网络状态肯定通过广播接收者实现,广播接受者:
/** * @Title NetworkStateReceiver * @Package com.ta.util.netstate * @Description 是一个检测网络状态改变的,需要配置 <receiver * android:name="com.ta.util.netstate.TANetworkStateReceiver" > * <intent-filter> <action * android:name="android.net.conn.CONNECTIVITY_CHANGE" /> <action * android:name="android.gzcpc.conn.CONNECTIVITY_CHANGE" /> * </intent-filter> </receiver> * * 需要开启权限 <uses-permission * android:name="android.permission.CHANGE_NETWORK_STATE" /> * <uses-permission * android:name="android.permission.CHANGE_WIFI_STATE" /> * <uses-permission * android:name="android.permission.ACCESS_NETWORK_STATE" /> * <uses-permission * android:name="android.permission.ACCESS_WIFI_STATE" /> * @author 白猫 * @date 2013-5-5 下午 22:47 * @version V1.2 */ public class TANetworkStateReceiver extends BroadcastReceiver { private static Boolean networkAvailable = false; private static netType netType; private static ArrayList<TANetChangeObserver> netChangeObserverArrayList = new ArrayList<TANetChangeObserver>(); private final static String ANDROID_NET_CHANGE_ACTION = "android.net.conn.CONNECTIVITY_CHANGE"; public final static String TA_ANDROID_NET_CHANGE_ACTION = "ta.android.net.conn.CONNECTIVITY_CHANGE"; private static BroadcastReceiver receiver; private static BroadcastReceiver getReceiver() { if (receiver == null) { receiver = new TANetworkStateReceiver(); } return receiver; } @Override public void onReceive(Context context, Intent intent) { receiver = TANetworkStateReceiver.this; if (intent.getAction().equalsIgnoreCase(ANDROID_NET_CHANGE_ACTION) || intent.getAction().equalsIgnoreCase( TA_ANDROID_NET_CHANGE_ACTION)) { TALogger.i(TANetworkStateReceiver.this, "网络状态改变."); if (!TANetWorkUtil.isNetworkAvailable(context)) { TALogger.i(TANetworkStateReceiver.this, "没有网络连接."); networkAvailable = false; } else { TALogger.i(TANetworkStateReceiver.this, "网络连接成功."); netType = TANetWorkUtil.getAPNType(context); networkAvailable = true; } notifyObserver(); } } /** * 注册网络状态广播 * * @param mContext */ public static void registerNetworkStateReceiver(Context mContext) { IntentFilter filter = new IntentFilter(); filter.addAction(TA_ANDROID_NET_CHANGE_ACTION); filter.addAction(ANDROID_NET_CHANGE_ACTION); mContext.getApplicationContext() .registerReceiver(getReceiver(), filter); } /** * 检查网络状态 * * @param mContext */ public static void checkNetworkState(Context mContext) { Intent intent = new Intent(); intent.setAction(TA_ANDROID_NET_CHANGE_ACTION); mContext.sendBroadcast(intent); } /** * 注销网络状态广播 * * @param mContext */ public static void unRegisterNetworkStateReceiver(Context mContext) { if (receiver != null) { try { mContext.getApplicationContext().unregisterReceiver(receiver); } catch (Exception e) { // TODO: handle exception TALogger.d("TANetworkStateReceiver", e.getMessage()); } } } /** * 获取当前网络状态,true为网络连接成功,否则网络连接失败 * * @return */ public static Boolean isNetworkAvailable() { return networkAvailable; } public static netType getAPNType() { return netType; } private void notifyObserver() { for (int i = 0; i < netChangeObserverArrayList.size(); i++) { TANetChangeObserver observer = netChangeObserverArrayList.get(i); if (observer != null) { if (isNetworkAvailable()) { observer.onConnect(netType); } else { observer.onDisConnect(); } } } } /** * 注册网络连接观察者 * * @param observerKey * observerKey */ public static void registerObserver(TANetChangeObserver observer) { if (netChangeObserverArrayList == null) { netChangeObserverArrayList = new ArrayList<TANetChangeObserver>(); } netChangeObserverArrayList.add(observer); } /** * 注销网络连接观察者 * * @param resID * observerKey */ public static void removeRegisterObserver(TANetChangeObserver observer) { if (netChangeObserverArrayList != null) { netChangeObserverArrayList.remove(observer); } } }
2、广播接受者接收到广播后,是通过设置好的观察者,传递回信息的,观察者:
/** * @Title TANetChangeObserver * @Package com.ta.util.netstate * @Description 是检测网络改变的观察者 * @author 白猫 * @date 2013-1-22 下午 9:35 * @version V1.0 */ public class TANetChangeObserver { /** * 网络连接连接时调用 */ public void onConnect(netType type) { } /** * 当前没有网络连接 */ public void onDisConnect() { } }
3、观察者,就是一个监听接口。广播接收者里用到的工具类:
/** * @Title NetWorkUtil * @Package com.ta.util.netstate * @Description 是检测网络的一个工具包 * @author 白猫 * @date 2013-1-22 下午 9:35 * @version V1.0 */ public class TANetWorkUtil { public static enum netType { wifi, CMNET, CMWAP, noneNet } /** * 网络是否可用 * * @param context * @return */ public static boolean isNetworkAvailable(Context context) { ConnectivityManager mgr = (ConnectivityManager) context .getSystemService(Context.CONNECTIVITY_SERVICE); NetworkInfo[] info = mgr.getAllNetworkInfo(); if (info != null) { for (int i = 0; i < info.length; i++) { if (info[i].getState() == NetworkInfo.State.CONNECTED) { return true; } } } return false; } /** * 判断是否有网络连接 * * @param context * @return */ public static boolean isNetworkConnected(Context context) { if (context != null) { ConnectivityManager mConnectivityManager = (ConnectivityManager) context .getSystemService(Context.CONNECTIVITY_SERVICE); NetworkInfo mNetworkInfo = mConnectivityManager .getActiveNetworkInfo(); if (mNetworkInfo != null) { return mNetworkInfo.isAvailable(); } } return false; } /** * 判断WIFI网络是否可用 * * @param context * @return */ public static boolean isWifiConnected(Context context) { if (context != null) { ConnectivityManager mConnectivityManager = (ConnectivityManager) context .getSystemService(Context.CONNECTIVITY_SERVICE); NetworkInfo mWiFiNetworkInfo = mConnectivityManager .getNetworkInfo(ConnectivityManager.TYPE_WIFI); if (mWiFiNetworkInfo != null) { return mWiFiNetworkInfo.isAvailable(); } } return false; } /** * 判断MOBILE网络是否可用 * * @param context * @return */ public static boolean isMobileConnected(Context context) { if (context != null) { ConnectivityManager mConnectivityManager = (ConnectivityManager) context .getSystemService(Context.CONNECTIVITY_SERVICE); NetworkInfo mMobileNetworkInfo = mConnectivityManager .getNetworkInfo(ConnectivityManager.TYPE_MOBILE); if (mMobileNetworkInfo != null) { return mMobileNetworkInfo.isAvailable(); } } return false; } /** * 获取当前网络连接的类型信息 * * @param context * @return */ public static int getConnectedType(Context context) { if (context != null) { ConnectivityManager mConnectivityManager = (ConnectivityManager) context .getSystemService(Context.CONNECTIVITY_SERVICE); NetworkInfo mNetworkInfo = mConnectivityManager .getActiveNetworkInfo(); if (mNetworkInfo != null && mNetworkInfo.isAvailable()) { return mNetworkInfo.getType(); } } return -1; } /** * * @author 白猫 * * 获取当前的网络状态 -1:没有网络 1:WIFI网络2:wap 网络3:net网络 * * @param context * * @return */ public static netType getAPNType(Context context) { ConnectivityManager connMgr = (ConnectivityManager) context .getSystemService(Context.CONNECTIVITY_SERVICE); NetworkInfo networkInfo = connMgr.getActiveNetworkInfo(); if (networkInfo == null) { return netType.noneNet; } int nType = networkInfo.getType(); if (nType == ConnectivityManager.TYPE_MOBILE) { if (networkInfo.getExtraInfo().toLowerCase().equals("cmnet")) { return netType.CMNET; } else { return netType.CMWAP; } } else if (nType == ConnectivityManager.TYPE_WIFI) { return netType.wifi; } return netType.noneNet; } }
好了,网络监听就用到这三个类,来看看ThinkAndroid是怎么用的:
由于网络监听是是对所有页面起效,所以在Application创建的时候给广播接收者设置一个全局的观察者:
taNetChangeObserver = new TANetChangeObserver() { @Override public void onConnect(netType type) { // TODO Auto-generated method stub super.onConnect(type); TAApplication.this.onConnect(type); } @Override public void onDisConnect() { // TODO Auto-generated method stub super.onDisConnect(); TAApplication.this.onDisConnect(); } }; TANetworkStateReceiver.registerObserver(taNetChangeObserver);
/** * 当前没有网络连接 */ public void onDisConnect() { networkAvailable = false; if (currentActivity != null) { currentActivity.onDisConnect(); } } /** * 网络连接连接时调用 */ protected void onConnect(netType type) { // TODO Auto-generated method stub networkAvailable = true; if (currentActivity != null) { currentActivity.onConnect(type); } }
郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。