Android Data Analyse(1)--ConnectivityManager

Android Data Analyse(1)–ConnectivityManager

   ConnectivityManager 主要的作用是管理与网络相关的状态 &查询一些网络信息,例如得到IP & networkinfo等。
   个人感觉其使用方法、调用方法、需求时刻均与TelephonyManager很类似。 
   TelephonyManager供其他应用调用查询一些设备等于simcard相关的信息。
   ConnectivityManager则是提供一些接口,让其他应用来查询网络状态相关的信息。

代码基本介绍

调用方法

ConnectivityManager mCM = (ConnectivityManager)
   context.getSystemService(Context.CONNECTIVITY_SERVICE);

常用的接口

接口返回值说明
getActiveLinkPropertiesLinkProperties返回当前默认网络的IP信息(也可以传入不同的参数如network获得特定网络的IP信息)
getActiveNetworkNetwork返回当前默认激活状态的网络(也可以传入不同的参数如UID来获得特定的激活的网络)
getActiveNetworkInfoNetworkInfo返回当前激活状态网络的信息
getAllNetworksNetwork[]返回当前所有网络
getAllNetworkInfoNetworkInfo[]返回当前所有网络的信息

定义网络类型

   /**
     * The Mobile data connection.  When active, all data traffic
     * will use this network type's interface by default
     * (it has a default route)
     */
   public static final int TYPE_MOBILE      = 0;
    /**
     * The WIFI data connection.  When active, all data traffic
     * will use this network type's interface by default
     * (it has a default route).
     */
    public static final int TYPE_WIFI        = 1;
    /**
     * An MMS-specific Mobile data connection.  This network type may use the
     * same network interface as {@link #TYPE_MOBILE} or it may use a different
     * one.  This is used by applications needing to talk to the carrier's
     * Multimedia Messaging Service servers.
     *
     * @deprecated Applications should instead use
     *         {@link #requestNetwork(NetworkRequest, NetworkCallback)} to request a network that
     *         provides the {@link NetworkCapabilities#NET_CAPABILITY_MMS} capability.
     */
    public static final int TYPE_MOBILE_MMS  = 2;
    /**
     * A SUPL-specific Mobile data connection.  This network type may use the
     * same network interface as {@link #TYPE_MOBILE} or it may use a different
     * one.  This is used by applications needing to talk to the carrier's
     * Secure User Plane Location servers for help locating the device.
     *
     * @deprecated Applications should instead use
     *         {@link #requestNetwork(NetworkRequest, NetworkCallback)} to request a network that
     *         provides the {@link NetworkCapabilities#NET_CAPABILITY_SUPL} capability.
     */
    public static final int TYPE_MOBILE_SUPL = 3;
    /**
     * A DUN-specific Mobile data connection.  This network type may use the
     * same network interface as {@link #TYPE_MOBILE} or it may use a different
     * one.  This is sometimes by the system when setting up an upstream connection
     * for tethering so that the carrier is aware of DUN traffic.
     */
    public static final int TYPE_MOBILE_DUN  = 4;
    /**
     * A High Priority Mobile data connection.  This network type uses the
     * same network interface as {@link #TYPE_MOBILE} but the routing setup
     * is different.
     *
     * @deprecated Applications should instead use
     *         {@link #requestNetwork(NetworkRequest, NetworkCallback)} to request a network that
     *         uses the {@link NetworkCapabilities#TRANSPORT_CELLULAR} transport.
     */
    public static final int TYPE_MOBILE_HIPRI = 5;
    /**
     * The WiMAX data connection.  When active, all data traffic
     * will use this network type's interface by default
     * (it has a default route).
     */
    public static final int TYPE_WIMAX       = 6;

    /**
     * The Bluetooth data connection.  When active, all data traffic
     * will use this network type's interface by default
     * (it has a default route).
     */
    public static final int TYPE_BLUETOOTH   = 7;

    /**
     * Dummy data connection.  This should not be used on shipping devices.
     */
    public static final int TYPE_DUMMY       = 8;

    /**
     * The Ethernet data connection.  When active, all data traffic
     * will use this network type's interface by default
     * (it has a default route).
     */
    public static final int TYPE_ETHERNET    = 9;

    /**
     * Over the air Administration.
     * {@hide}
     */
    public static final int TYPE_MOBILE_FOTA = 10;

    /**
     * IP Multimedia Subsystem.
     * {@hide}
     */
    public static final int TYPE_MOBILE_IMS  = 11;

    /**
     * Carrier Branded Services.
     * {@hide}
     */
    public static final int TYPE_MOBILE_CBS  = 12;

    /**
     * A Wi-Fi p2p connection. Only requesting processes will have access to
     * the peers connected.
     * {@hide}
     */
    public static final int TYPE_WIFI_P2P    = 13;

    /**
     * The network to use for initially attaching to the network
     * {@hide}
     */
    public static final int TYPE_MOBILE_IA = 14;

    /**
     * Emergency PDN connection for emergency services.  This
     * may include IMS and MMS in emergency situations.
     * {@hide}
     */
    public static final int TYPE_MOBILE_EMERGENCY = 15;

    /**
     * The network that uses proxy to achieve connectivity.
     * {@hide}
     */
    public static final int TYPE_PROXY = 16;

    /**
     * A virtual network using one or more native bearers.
     * It may or may not be providing security services.
     */
    public static final int TYPE_VPN = 17;

系统代码运用代码

    /**
     * 在拨打电话的时候,判断是否应该弹出wifi call的dialog
     * 
     */
    public static boolean shallShowWifiCallDialog(final Context context) {
        boolean wifiCallTurnOn = isWifiCallTurnOnEnabled(context);

//获取ConnectivityManager对象
        ConnectivityManager mCM= (ConnectivityManager)  context.getSystemService(Context.CONNECTIVITY_SERVICE);

//获取当前wifi的网络信息
        NetworkInfo wifiNetworkInfo = conManager.getNetworkInfo(ConnectivityManager.TYPE_WIFI);

//判断当前wifi信息是否可用 & wifi是否连接
        boolean wifiAvailableNotConnected =
                wifiNetworkInfo.isAvailable() && !wifiNetworkInfo.isConnected();

        return wifiCallTurnOn && wifiAvailableNotConnected && !isCellularNetworkAvailable(context);
    }

Demo 代码


    /**
     * 判断当前RCS service 是否可用 
     */
    private void showFailureMessage() {
        //获得ConnectivityManager 对象
        ConnectivityManager cm = (ConnectivityManager)
        mContext.getSystemService(Context.CONNECTIVITY_SERVICE);
        //获得当前所有active的网络
        NetworkInfo activeNetwork = cm.getActiveNetworkInfo();

        if ((activeNetwork == null) 
             ||(activeNetwork.getType() 
             != ConnectivityManager.TYPE_MOBILE)) {
            Toast toast =  Toast.makeText(mContext.getApplicationContext(),
                    R.string.rcsunavailable, Toast.LENGTH_SHORT);
            toast.show();
        }
    }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值