Android Data Analyse(1)–ConnectivityManager
ConnectivityManager 主要的作用是管理与网络相关的状态 &查询一些网络信息,例如得到IP & networkinfo等。
个人感觉其使用方法、调用方法、需求时刻均与TelephonyManager很类似。
TelephonyManager供其他应用调用查询一些设备等于simcard相关的信息。
ConnectivityManager则是提供一些接口,让其他应用来查询网络状态相关的信息。
代码基本介绍
调用方法:
ConnectivityManager mCM = (ConnectivityManager)
context.getSystemService(Context.CONNECTIVITY_SERVICE);
常用的接口:
接口 | 返回值 | 说明 |
---|---|---|
getActiveLinkProperties | LinkProperties | 返回当前默认网络的IP信息(也可以传入不同的参数如network获得特定网络的IP信息) |
getActiveNetwork | Network | 返回当前默认激活状态的网络(也可以传入不同的参数如UID来获得特定的激活的网络) |
getActiveNetworkInfo | NetworkInfo | 返回当前激活状态网络的信息 |
getAllNetworks | Network[] | 返回当前所有网络 |
getAllNetworkInfo | NetworkInfo[] | 返回当前所有网络的信息 |
… | … | … |
定义网络类型:
/**
* 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();
}
}