您好,登錄后才能下訂單哦!
如何在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進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。