天天看點

ConnectivityManager源碼分析

先來看一下關于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
 * {@link android.content.Context#getSystemService(String) Context.getSystemService(Context.CONNECTIVITY_SERVICE)}.
 * <p>
 * The primary responsibilities of this class are to:
 * <ol>
 * <li>Monitor network connections (Wi-Fi, GPRS, UMTS, etc.)</li>
 * <li>Send broadcast intents when network connectivity changes</li>
 * <li>Attempt to "fail over" to another network when connectivity to a network
 * is lost</li>
 * <li>Provide an API that allows applications to query the coarse-grained or fine-grained
 * state of the available networks</li>
 * </ol>
 */
           

從上面的介紹中總結出該類的作用:

 1.追蹤網絡的連接配接狀态:WI-Fi,GPRS,UMTS,etc;

 2.當網絡狀态發生改變時,發送廣播sendBroadcast消息;

 3.失效轉移file over:當A網絡斷掉時,系統自動切換,B網絡及時補上提供服務,使用者不會察覺網絡的改變;

 4.提供查詢的API:(我們一般使用兩種:)連沒連網絡和連的是什麼類型的網絡(getNetworkInfo(network_type))。

我們在需要監控網絡狀态時要注冊廣播接收器,BroadcastReceiver Action是:

public static final String CONNECTIVITY_ACTION = "android.net.conn.CONNECTIVITY_CHANGE";
           

看下注釋

/**
     * A change in network connectivity has occurred. A connection has either
     * been established or lost. The NetworkInfo for the affected network is
     * sent as an extra; it should be consulted to see what kind of
     * connectivity event occurred.
     * <p/>
     * If this is a connection that was the result of failing over from a
     * disconnected network, then the FAILOVER_CONNECTION boolean extra is
     * set to true.
     * <p/>
     * For a loss of connectivity, if the connectivity manager is attempting
     * to connect (or has already connected) to another network, the
     * NetworkInfo for the new network is also passed as an extra. This lets
     * any receivers of the broadcast know that they should not necessarily
     * tell the user that no data traffic will be possible. Instead, the
     * receiver should expect another broadcast soon, indicating either that
     * the failover attempt succeeded (and so there is still overall data
     * connectivity), or that the failover attempt failed, meaning that all
     * connectivity has been lost.
     * <p/>
     * For a disconnect event, the boolean extra EXTRA_NO_CONNECTIVITY
     * is set to {@code true} if there are no connected networks at all.
     */
           

從上面我們可以得到系統網絡狀态廣播的情況:

    1.網絡的連接配接與斷開

     2.失效轉移file over,其對應的标志FAILOVER_CONNECTION=true}

當無網絡時,EXTRA_NO_CONNECTIVITY=true。

我們在查詢網絡狀态時經常要區分出網絡的類型:

public static final int TYPE_NONE        = -1;
    public static final int TYPE_MOBILE      = 0;    //基本通話用的網絡,譬如3G,GPRS
    public static final int TYPE_WIFI        = 1;
    public static final int TYPE_MOBILE_MMS  = 2;    //彩信網絡
    public static final int TYPE_MOBILE_SUPL = 3;    //是一種基于标準、允許行動電話使用者與定位伺服器通信的協定
    public static final int TYPE_MOBILE_DUN  = 4;    //網絡橋接,很老的一個網絡
    public static final int TYPE_MOBILE_HIPRI = 5;   //高優先級的移動資料連接配接。相同的為{TYPE_MOBILE},但路由的設定是不同的。隻有請求的程序将有機會獲得移動的DNS伺服器。
    public static final int TYPE_WIMAX       = 6;    //全球微波互聯接入
    public static final int TYPE_BLUETOOTH   = 7;    //藍牙
    public static final int TYPE_DUMMY       = 8;    //虛拟連接配接
    public static final int TYPE_ETHERNET    = 9;    //以太網 
    public static final int TYPE_MOBILE_FOTA = 10;
    public static final int TYPE_MOBILE_IMS  = 11;
    public static final int TYPE_MOBILE_CBS  = 12;
    public static final int TYPE_WIFI_P2P    = 13;   //通過wifi直連wifi
    public static final int TYPE_MOBILE_IA = 14;
           

TYPE_MOBILE_MMS,TYPE_MOBILE_SUPL,TYPE_MOBILE_DUN,TYPE_MOBILE_HIPRI,TYPE_MOBILE_FOTA,TYPE_MOBILE_IMS,TYPE_MOBILE_CBS,TYPE_MOBILE_A是屬于MOBILE類型的。

TYPE_WIFI,case TYPE_WIFI_P2P是屬于WIFI類型的。

我們在使用getNetworkInfo(networkType)來獲得指定網絡類型的狀态資訊。

public NetworkInfo getNetworkInfo(int networkType) {

        try {

            return mService.getNetworkInfo(networkType);

        } catch (RemoteException e) {

            return null;

        }

    }

傳回值類型NetworkInfo中可以通過getType()獲得的網絡類型,可以通過getState()獲得網絡狀态,狀态值有下面幾個:CONNECTING, CONNECTED, SUSPENDED,

DISCONNECTING, DISCONNECTED, UNKNOWN。

繼續閱讀