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。

    原文作者:u010852801
    原文地址: https://blog.csdn.net/u010852801/article/details/44117253
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞