如何在Android中使用Observer监听网络状态?针对这个问题,这篇文章详细介绍了相对应的分析和解答,希望可以帮助更多想解决这个问题的小伙伴找到更简单易行的方法。
观察者模式属于面向对象的23中设计模式之一,不了解的同学请自行Google
既然用观察者模式,自然离不开观察者模式里最重要的两个类Subject和Ovserver了
Subjcet接口:
/** * Description: observer subject * author: Wang * date: 11/28/16 11:19 AM * * Copyright©2016 by wang. All rights reserved. */ public interface NetConnectionSubject { /** * 注册观察者 * * @param observer */ public void addNetObserver(NetConnectionObserver observer); /** * 移除观察者 * * @param observer */ public void removeNetObserver(NetConnectionObserver observer); /** * 状态更新通知 * * @param type */ public void notifyNetObserver(int type); }
Observer接口:
/** * Description: observer * author: Wang * date: 11/28/16 11:20 AM * * Copyright©2016 by wang. All rights reserved. */ public interface NetConnectionObserver { /** * 通知观察者更改状态 * * @param type */ public void updateNetStatus(int type); }
在Android里,最适合实现Subject类的,莫过于Application了,因为它全局唯一而且生命周期就是这个App的生命周期:
/** * Description: App's application should extend this class * author: Wang * date: 11/28/16 10:34 AM * * Copyright©2016 by wang. All rights reserved. */ public abstract class BaseApplication extends Application implements NetConnectionSubject { protected static BaseApplication instance; private int currentNetType = -1; private List<NetConnectionObserver> observers = new ArrayList<>(); public static BaseApplication getInstance() { return instance; } /** * current net connection type * * @return */ public int getCurrentNetType() { return currentNetType; } /** * current net connection status * * @return */ public boolean isNetConnection() { return currentNetType == NetWorkUtil.NET_NO_CONNECTION ? false : true; } @Override public void onCreate() { super.onCreate(); instance = this; currentNetType = NetWorkUtil.getConnectionType(this); } @Override public void addNetObserver(NetConnectionObserver observer) { if (!observers.contains(observer)) { observers.add(observer); } } @Override public void removeNetObserver(NetConnectionObserver observer) { if (observers != null && observers.contains(observer)) { observers.remove(observer); } } @Override public void notifyNetObserver(int type) { /** * 避免多次发送相同的网络状态 */ if (currentNetType == type) { return; } else { currentNetType = type; if (observers != null && observers.size() > 0) { for (NetConnectionObserver observer : observers) { observer.updateNetStatus(type); } } } } }
具体谁要实现Observer接口,就要看具体场景了,这里以Activity为栗子吧:
/** * Description: TODO * author: WangKunHui * date: 16/12/30 下午3:08 * <p> * Copyright©2016 by wang. All rights reserved. */ public class TestActivity extends Activity implements NetConnectionObserver { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); /**省略一些方法**/ BaseApplication.getInstance().addNetObserver(this); } @Override public void updateNetStatus(int type) { //当监听网络状态发生变化 这里会及时的收到回馈 } @Override protected void onDestroy() { super.onDestroy(); BaseApplication.getInstance().removeNetObserver(this); } }
这里有个地方一定要注意:当Activity销毁的时候,一定要把这个观察者从观察者队列里移除掉!否者会发生内存泄漏
到这里,观察者模式已经写完了,谢谢收看。
读者:你是不是忘了点什么,说好的网络监听呢?
我:Easy easy~ 刚刚只不过是中场休息
如果只有上面那么多的话,是不能监听网络状态的,想要监听网络状态的变化,还得靠我们的广播接收者啊,有请:
/** * Description: 网络连接状态的监听 * author: Wang * date: 16/8/3 下午10:54 * * Copyright©2016 by wang. All rights reserved. */ public class NetConnectionReceiver extends BroadcastReceiver { @Override public void onReceive(Context context, Intent intent) { if (intent.getAction().equals(ConnectivityManager.CONNECTIVITY_ACTION)) { int connectionType = NetWorkUtil.getConnectionType(context); /** * 更改网络状态 */ if (BaseApplication.getInstance() != null) { BaseApplication.getInstance().notifyNetObserver(connectionType); } } } }
NetWorkUtil:
/** * @author Wang * @version 1.0.0 * @description 网络操作工具类 * @create 2014-2-18 上午09:22:30 * @company */ public class NetWorkUtil { /** * 无网络链接 */ public static final int NET_NO_CONNECTION = 0; /** * wifi */ public static final int NET_TYPE_WIFI = 1; public static final int NET_TYPE_2G = 2; public static final int NET_TYPE_3G = 3; public static final int NET_TYPE_4G = 4; /** * 未知的网络类型 */ public static final int NET_TYPE_UNKNOWN = 5; /** * 获取网络类型 * * @param context * @return */ public static int getConnectionType(Context context) { int netType = NET_NO_CONNECTION; NetworkInfo networkInfo = ((ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE)).getActiveNetworkInfo(); if (networkInfo == null) { netType = NET_NO_CONNECTION; } else { if (networkInfo.getType() == ConnectivityManager.TYPE_WIFI) { netType = NET_TYPE_WIFI; } else { int networkType = networkInfo.getSubtype(); switch (networkType) { case TelephonyManager.NETWORK_TYPE_GPRS: case TelephonyManager.NETWORK_TYPE_EDGE: case TelephonyManager.NETWORK_TYPE_CDMA: case TelephonyManager.NETWORK_TYPE_1xRTT: case TelephonyManager.NETWORK_TYPE_IDEN: //api<8 : replace by 11 netType = NET_TYPE_2G; break; case TelephonyManager.NETWORK_TYPE_UMTS: case TelephonyManager.NETWORK_TYPE_EVDO_0: case TelephonyManager.NETWORK_TYPE_EVDO_A: case TelephonyManager.NETWORK_TYPE_HSDPA: case TelephonyManager.NETWORK_TYPE_HSUPA: case TelephonyManager.NETWORK_TYPE_HSPA: case TelephonyManager.NETWORK_TYPE_EVDO_B: //api<9:replace by 14 case TelephonyManager.NETWORK_TYPE_EHRPD: //api<11:replace by 12 case TelephonyManager.NETWORK_TYPE_HSPAP: //api<13:replace by 15 netType = NET_TYPE_3G; break; case TelephonyManager.NETWORK_TYPE_LTE: //api<11:replace by 13 netType = NET_TYPE_4G; break; default: String subType = networkInfo.getSubtypeName(); if (subType.equalsIgnoreCase("TD-SCDMA") || subType.equalsIgnoreCase("WCDMA") || subType.equalsIgnoreCase("CDMA2000")) { netType = NET_TYPE_3G; } else { netType = NET_TYPE_UNKNOWN; } break; } } } return netType; } }
Android是一种基于Linux内核的自由及开放源代码的操作系统,主要使用于移动设备,如智能手机和平板电脑,由美国Google公司和开放手机联盟领导及开发。
关于如何在Android中使用Observer监听网络状态问题的解答就分享到这里了,希望以上内容可以对大家有一定的帮助,如果你还有很多疑惑没有解开,可以关注亿速云行业资讯频道了解更多相关知识。
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。