最近發現做項目監聽網絡切換廣播,根據網絡條件切換一些設置.測試發現每次3G-WIFI 或者WIFI到3G,網絡切換的廣播都會發出多次.比如3G-->WIFI
會發送三個廣播 1.連接wifi 2.關閉手機網絡 3.連接wifi 有沒有方法判斷這個過程呢?那就來看一個類
http://www.androidcommunitydocs.com/reference/android/net/ConnectivityManager.html (官方API 文檔)
public class
ConnectivityManager extends Object
java.lang.Object
↳ android.net.ConnectivityManager
Class that answers queries about the state of network connectivity. It also notifies applications when network connectivity changes. Get an instance of this class by calling Context.getSystemService(Context.CONNECTIVITY_SERVICE).
The primary responsibilities of this class are to 該類主要職責:
- Monitor network connections (Wi-Fi, GPRS, UMTS, etc.) 監視網絡連接
- Send broadcast intents when network connectivity changes 網絡連接變化時發送廣播
- Attempt to "fail over" to another network when connectivity to a network is lost 當當前網絡不可用時嘗試切換到其他網絡
- Provide an API that allows applications to query the coarse-grained or fine-grained state of the available networks 提供API給應用查詢粗略或精細的網絡狀態
(http://developer.android.com/training/monitoring-device-state/connectivity-monitoring.html)
有了這些接着我們打印一下,在接受到網絡變化的廣播時.傳過來的網絡狀態與當前激活的網絡狀態有什么不同 代碼如下 :
1 @Override 2 public void onReceive(Context context, Intent intent) { 3 String action = intent.getAction(); 4 Log.i("MyReceiver", action); 5 if(ConnectivityManager.CONNECTIVITY_ACTION.equals(action)){ 6 Bundle b = intent.getExtras(); 7 if(b == null){ 8 Log.i("MyReceiver", "b == null "); 9 return ; 10 } 11 NetworkInfo netInfo = (NetworkInfo) b.get(ConnectivityManager.EXTRA_NETWORK_INFO); 12 NetworkInfo.State state = netInfo.getState(); 13 int netInfoType = netInfo.getType(); 14 15 ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE); 16 if(cm == null){ 17 Log.i("MyReceiver", "ConnectivityManager == null"); 18 return ; 19 } 20 NetworkInfo activeNetInfo = cm.getActiveNetworkInfo(); 21 if(state != null){ 22 Log.i("MyReceiver", state.name()); 23 Log.i("MyReceiver", "state : " + netInfo.getTypeName() + " : " + netInfo.getType()); 24 }else{ 25 Log.i("MyReceiver", "state == null"); 26 } 27 28 29 if(activeNetInfo != null){ 30 int activeNetType = activeNetInfo.getType(); 31 Log.i("MyReceiver", activeNetInfo.getTypeName() + " : " + activeNetInfo.getType()); 32 }else{
Log.i("MyReceiver", "activeNetInfo == null "); 43 } 45 46 } 47 48 }
使用手機進行網絡間切換,打印如下 :
wifi ---> 3G
03-17 17:56:45.526: I/MyReceiver(13406): android.net.conn.CONNECTIVITY_CHANGE
03-17 17:56:45.526: I/MyReceiver(13406): DISCONNECTED
03-17 17:56:45.526: I/MyReceiver(13406): state : mobile : 0
03-17 17:56:45.531: I/MyReceiver(13406): activeNetInfo == null
03-17 17:56:45.991: I/MyReceiver(13406): android.net.conn.CONNECTIVITY_CHANGE
03-17 17:56:45.991: I/MyReceiver(13406): CONNECTED
03-17 17:56:45.991: I/MyReceiver(13406): state : WIFI : 1
03-17 17:56:45.991: I/MyReceiver(13406): activeNetInfo == null
03-17 17:56:49.491: I/MyReceiver(13406): android.net.conn.CONNECTIVITY_CHANGE
03-17 17:56:49.496: I/MyReceiver(13406): DISCONNECTED
03-17 17:56:49.496: I/MyReceiver(13406): state : WIFI : 1
03-17 17:56:49.496: I/MyReceiver(13406): mobile : 0
03-17 17:56:49.966: I/MyReceiver(13406): android.net.conn.CONNECTIVITY_CHANGE
03-17 17:56:49.966: I/MyReceiver(13406): CONNECTED
03-17 17:56:49.966: I/MyReceiver(13406): state : mobile : 0
03-17 17:56:49.966: I/MyReceiver(13406): mobile : 0
3G-->wifi
03-17 18:00:35.286: I/MyReceiver(13406): android.net.conn.CONNECTIVITY_CHANGE
03-17 18:00:35.286: I/MyReceiver(13406): CONNECTED
03-17 18:00:35.286: I/MyReceiver(13406): state : WIFI : 1
03-17 18:00:35.291: I/MyReceiver(13406): WIFI : 1
03-17 18:00:40.641: I/MyReceiver(13406): android.net.conn.CONNECTIVITY_CHANGE
03-17 18:00:40.641: I/MyReceiver(13406): DISCONNECTED
03-17 18:00:40.641: I/MyReceiver(13406): state : mobile : 0
03-17 18:00:40.641: I/MyReceiver(13406): WIFI : 1
03-17 18:00:41.506: I/MyReceiver(13406): android.net.conn.CONNECTIVITY_CHANGE
03-17 18:00:41.506: I/MyReceiver(13406): CONNECTED
03-17 18:00:41.506: I/MyReceiver(13406): state : WIFI : 1
03-17 18:00:41.506: I/MyReceiver(13406): WIFI : 1
wifi --3G --WIFI
03-17 18:02:54.861: I/MyReceiver(13406): android.net.conn.CONNECTIVITY_CHANGE
03-17 18:02:54.861: I/MyReceiver(13406): DISCONNECTED
03-17 18:02:54.861: I/MyReceiver(13406): state : WIFI : 1
03-17 18:02:54.861: I/MyReceiver(13406): WIFI : 1
03-17 18:02:55.331: I/MyReceiver(13406): android.net.conn.CONNECTIVITY_CHANGE
03-17 18:02:55.331: I/MyReceiver(13406): CONNECTED
03-17 18:02:55.331: I/MyReceiver(13406): state : mobile : 0
03-17 18:02:55.331: I/MyReceiver(13406): WIFI : 1
03-17 18:02:56.771: I/MyReceiver(13406): android.net.conn.CONNECTIVITY_CHANGE
03-17 18:02:56.771: I/MyReceiver(13406): CONNECTED
03-17 18:02:56.771: I/MyReceiver(13406): state : WIFI : 1
03-17 18:02:56.771: I/MyReceiver(13406): WIFI : 1
03-17 18:02:57.171: I/MyReceiver(13406): android.net.conn.CONNECTIVITY_CHANGE
03-17 18:02:57.171: I/MyReceiver(13406): DISCONNECTED
03-17 18:02:57.171: I/MyReceiver(13406): state : mobile : 0
03-17 18:02:57.171: I/MyReceiver(13406): WIFI : 1
03-17 18:02:57.771: I/MyReceiver(13406): android.net.conn.CONNECTIVITY_CHANGE
03-17 18:02:57.771: I/MyReceiver(13406): CONNECTED
03-17 18:02:57.771: I/MyReceiver(13406): state : WIFI : 1
03-17 18:02:57.771: I/MyReceiver(13406): WIFI : 1
關閉wifi
03-17 18:08:06.381: I/MyReceiver(13406): android.net.conn.CONNECTIVITY_CHANGE
03-17 18:08:06.381: I/MyReceiver(13406): DISCONNECTED
03-17 18:08:06.381: I/MyReceiver(13406): state : WIFI : 1
03-17 18:08:06.386: I/MyReceiver(13406): activeNetInfo == null
關閉3G
03-17 18:09:23.366: I/MyReceiver(13406): android.net.conn.CONNECTIVITY_CHANGE
03-17 18:09:23.366: I/MyReceiver(13406): DISCONNECTED
03-17 18:09:23.371: I/MyReceiver(13406): state : mobile : 0
03-17 18:09:23.371: I/MyReceiver(13406): activeNetInfo == null
打開wifi
03-17 18:09:52.096: I/MyReceiver(13406): android.net.conn.CONNECTIVITY_CHANGE
03-17 18:09:52.096: I/MyReceiver(13406): CONNECTED
03-17 18:09:52.096: I/MyReceiver(13406): state : WIFI : 1
03-17 18:09:52.096: I/MyReceiver(13406): WIFI : 1
打開3G
03-17 18:10:42.976: I/MyReceiver(13406): android.net.conn.CONNECTIVITY_CHANGE
03-17 18:10:42.976: I/MyReceiver(13406): CONNECTED
03-17 18:10:42.976: I/MyReceiver(13406): state : mobile : 0
03-17 18:10:42.976: I/MyReceiver(13406): mobile : 0
根據這些狀態,在網絡切換的時候 我們可以總結出一些規律 :
1.網絡在切換之前處於斷開狀態時 activeNetInfo == null
2.當網絡狀態不同時一般處於中間狀態
3.網絡切換有可能發送重復的廣播 (是這樣嗎?求高手解答)
加入一些判斷然后測試代碼如下 :
@Override public void onReceive(Context context, Intent intent) { String action = intent.getAction(); Log.i("MyReceiver", action); if(ConnectivityManager.CONNECTIVITY_ACTION.equals(action)){ Bundle b = intent.getExtras(); if(b == null){ Log.i("MyReceiver", "b == null "); return ; } NetworkInfo netInfo = (NetworkInfo) b.get(ConnectivityManager.EXTRA_NETWORK_INFO); NetworkInfo.State state = netInfo.getState(); int netInfoType = netInfo.getType(); if(MyApplication.cacheState == null){ MyApplication.cacheState = state ; MyApplication.netType = netInfoType; // 移動網絡 (2G/3G/4G 間切換) or wifi }else if(MyApplication.cacheState == state && MyApplication.netType == netInfo.getType()){ Log.i("MyReceiver", "state : " + state.name() +" -- " + netInfo.getTypeName() + " : " + netInfo.getType()); Log.i("MyReceiver", " 相同 狀態廣播 "); return ; } ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE); if(cm == null){ Log.i("MyReceiver", "ConnectivityManager == null"); return ; } NetworkInfo activeNetInfo = cm.getActiveNetworkInfo(); if(activeNetInfo != null){ int activeNetType = activeNetInfo.getType(); Log.i("MyReceiver", activeNetInfo.getTypeName() + " : " + activeNetInfo.getType()); if(activeNetType != netInfoType){ // 類型不同 認為是中間狀態 不處理 Log.i("MyReceiver", "類型不同 判斷處於中間狀態 : 不處理 " ); }else{ MyApplication.cacheState = state ; MyApplication.netType = netInfoType; Log.i("MyReceiver", "當前操作 state : " + state.name() +" -- " + netInfo.getTypeName() + " : " + netInfo.getType()); } }else{ MyApplication.cacheState = state ; MyApplication.netType = netInfoType; Log.i("MyReceiver", "activeNetInfo == null "); Log.i("MyReceiver", "當前操作 state : " + state.name() +" -- " + netInfo.getTypeName() + " : " + netInfo.getType()); } } } }
測試了一下,已經滿足了我的要求.只是看起來方法有點笨. 應該會有更加簡潔准確的方法,希望有知道的朋友提供一下思路。