Android 網絡切換 發送屢次廣播問題

     

       最近發現作項目監聽網絡切換廣播,根據網絡條件切換一些設置.測試發現每次3G-WIFI 或者WIFI到3G,網絡切換的廣播都會發出屢次.好比3G-->WIFI html

       會發送三個廣播 1.鏈接wifi  2.關閉手機網絡 3.鏈接wifi  有沒有方法判斷這個過程呢?那就來看一個類 java

       http://www.androidcommunitydocs.com/reference/android/net/ConnectivityManager.html   (官方API 文檔)android

      public class網絡

        ConnectivityManager  extends Objectapp

            java.lang.Object
                ↳ android.net.ConnectivityManager
ide

        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 該類主要職責:this

  1. Monitor network connections (Wi-Fi, GPRS, UMTS, etc.)  監視網絡鏈接
  2. Send broadcast intents when network connectivity changes  網絡鏈接變化時發送廣播
  3. Attempt to "fail over" to another network when connectivity to a network is lost  噹噹前網絡不可用時嘗試切換到其餘網絡
  4. 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)spa

     有了這些接着咱們打印一下,在接受到網絡變化的廣播時.傳過來的網絡狀態與當前激活的網絡狀態有什麼不一樣 代碼以下 : .net

        

       

 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());
            }
            
        }

    }

}

 

  測試了一下,已經知足了個人要求.只是看起來方法有點笨. 應該會有更加簡潔準確的方法,但願有知道的朋友提供一下思路。

相關文章
相關標籤/搜索