public class ConnectivityManager extends Object
Context.getSystemService(Context.CONNECTIVITY_SERVICE)
.
The primary responsibilities of this class are to:
Modifier and Type | Field and Description |
---|---|
static String |
ACTION_BACKGROUND_DATA_SETTING_CHANGED
Deprecated.
As of
Build.VERSION_CODES.ICE_CREAM_SANDWICH , availability
of background data depends on several combined factors, and
this broadcast is no longer sent. Instead, when background
data is unavailable, getActiveNetworkInfo() will now
appear disconnected. During first boot after a platform
upgrade, this broadcast will be sent once if
getBackgroundDataSetting() was false before
the upgrade. |
static String |
ACTION_DATA_ACTIVITY_CHANGE
Broadcast action to indicate the change of data activity status
(idle or active) on a network in a recent period.
|
static String |
ACTION_TETHER_STATE_CHANGED
Broadcast Action: A tetherable connection has come or gone
TODO - finish the doc
|
static String |
CONNECTIVITY_ACTION
A change in network connectivity has occurred.
|
static String |
CONNECTIVITY_ACTION_IMMEDIATE
Identical to
CONNECTIVITY_ACTION broadcast, but sent without any
applicable Settings.Secure#CONNECTIVITY_CHANGE_DELAY . |
static int |
CONNECTIVITY_CHANGE_DELAY_DEFAULT
Default value for
Settings.Global#CONNECTIVITY_CHANGE_DELAY in
milliseconds. |
static int |
DEFAULT_NETWORK_PREFERENCE |
static String |
EXTRA_ACTIVE_TETHER |
static String |
EXTRA_AVAILABLE_TETHER |
static String |
EXTRA_DEVICE_TYPE
The lookup key for an enum that indicates the network device type on which this data activity
change happens.
|
static String |
EXTRA_ERRORED_TETHER |
static String |
EXTRA_EXTRA_INFO
The lookup key for a string that provides optionally supplied
extra information about the network state.
|
static String |
EXTRA_INET_CONDITION
The lookup key for an int that provides information about
our connection to the internet at large.
|
static String |
EXTRA_IS_ACTIVE
The lookup key for a boolean that indicates the device is active or not.
|
static String |
EXTRA_IS_FAILOVER
The lookup key for a boolean that indicates whether a connect event
is for a network to which the connectivity manager was failing over
following a disconnect on another network.
|
static String |
EXTRA_NETWORK_INFO
Deprecated.
Since
NetworkInfo can vary based on UID, applications
should always obtain network information through
getActiveNetworkInfo() or
getAllNetworkInfo() . |
static String |
EXTRA_NETWORK_TYPE
Network type which triggered a
CONNECTIVITY_ACTION broadcast. |
static String |
EXTRA_NO_CONNECTIVITY
The lookup key for a boolean that indicates whether there is a
complete lack of connectivity, i.e., no network is available.
|
static String |
EXTRA_OTHER_NETWORK_INFO
The lookup key for a
NetworkInfo object. |
static String |
EXTRA_REASON
The lookup key for a string that indicates why an attempt to connect
to a network failed.
|
static String |
INET_CONDITION_ACTION
Broadcast Action: The network connection may not be good
uses
ConnectivityManager.EXTRA_INET_CONDITION and
ConnectivityManager.EXTRA_NETWORK_INFO to specify
the network and it's condition. |
static int |
MAX_NETWORK_TYPE |
static int |
MAX_RADIO_TYPE |
static int |
TETHER_ERROR_DISABLE_NAT_ERROR |
static int |
TETHER_ERROR_ENABLE_NAT_ERROR |
static int |
TETHER_ERROR_IFACE_CFG_ERROR |
static int |
TETHER_ERROR_MASTER_ERROR |
static int |
TETHER_ERROR_NO_ERROR |
static int |
TETHER_ERROR_SERVICE_UNAVAIL |
static int |
TETHER_ERROR_TETHER_IFACE_ERROR |
static int |
TETHER_ERROR_UNAVAIL_IFACE |
static int |
TETHER_ERROR_UNKNOWN_IFACE |
static int |
TETHER_ERROR_UNSUPPORTED |
static int |
TETHER_ERROR_UNTETHER_IFACE_ERROR |
static int |
TYPE_BLUETOOTH
The Default Bluetooth data connection.
|
static int |
TYPE_DUMMY
Dummy data connection.
|
static int |
TYPE_ETHERNET
The Default Ethernet data connection.
|
static int |
TYPE_MOBILE
The Default Mobile data connection.
|
static int |
TYPE_MOBILE_CBS
Carrier Branded Services
|
static int |
TYPE_MOBILE_DUN
A DUN-specific Mobile data connection.
|
static int |
TYPE_MOBILE_FOTA
Over the air Adminstration.
|
static int |
TYPE_MOBILE_HIPRI
A High Priority Mobile data connection.
|
static int |
TYPE_MOBILE_IMS
IP Multimedia Subsystem
|
static int |
TYPE_MOBILE_MMS
An MMS-specific Mobile data connection.
|
static int |
TYPE_MOBILE_SUPL
A SUPL-specific Mobile data connection.
|
static int |
TYPE_NONE
The absence of APN..
|
static int |
TYPE_WIFI
The Default WIFI data connection.
|
static int |
TYPE_WIFI_P2P
A Wi-Fi p2p connection.
|
static int |
TYPE_WIMAX
The Default WiMAX data connection.
|
Constructor and Description |
---|
ConnectivityManager(IConnectivityManager service) |
Modifier and Type | Method and Description |
---|---|
void |
captivePortalCheckComplete(NetworkInfo info) |
static ConnectivityManager |
from(Context context) |
LinkProperties |
getActiveLinkProperties() |
NetworkInfo |
getActiveNetworkInfo()
Returns details about the currently active data network.
|
NetworkInfo |
getActiveNetworkInfoForUid(int uid) |
NetworkQuotaInfo |
getActiveNetworkQuotaInfo()
Return quota status for the current active network, or
null if no
network is active. |
NetworkInfo[] |
getAllNetworkInfo() |
boolean |
getBackgroundDataSetting()
Deprecated.
As of
Build.VERSION_CODES.ICE_CREAM_SANDWICH , availability of
background data depends on several combined factors, and this method will
always return true . Instead, when background data is unavailable,
getActiveNetworkInfo() will now appear disconnected. |
ProxyProperties |
getGlobalProxy() |
int |
getLastTetherError(String iface) |
LinkProperties |
getLinkProperties(int networkType) |
boolean |
getMobileDataEnabled()
Gets the value of the setting for enabling Mobile data.
|
NetworkInfo |
getNetworkInfo(int networkType) |
int |
getNetworkPreference() |
static String |
getNetworkTypeName(int type) |
ProxyProperties |
getProxy() |
String[] |
getTetherableBluetoothRegexs() |
String[] |
getTetherableIfaces() |
String[] |
getTetherableUsbRegexs() |
String[] |
getTetherableWifiRegexs() |
String[] |
getTetheredIfaces() |
String[] |
getTetheringErroredIfaces() |
boolean |
isActiveNetworkMetered()
Returns if the currently active data network is metered.
|
boolean |
isNetworkSupported(int networkType)
Returns true if the hardware supports the given network type
else it returns false.
|
static boolean |
isNetworkTypeMobile(int networkType) |
static boolean |
isNetworkTypeValid(int networkType) |
boolean |
isTetheringSupported() |
void |
reportInetCondition(int networkType,
int percentage) |
boolean |
requestNetworkTransitionWakelock(String forWhom)
Ensure the device stays awake until we connect with the next network
|
boolean |
requestRouteToHost(int networkType,
int hostAddress)
Ensure that a network route exists to deliver traffic to the specified
host via the specified network interface.
|
boolean |
requestRouteToHostAddress(int networkType,
InetAddress hostAddress)
Ensure that a network route exists to deliver traffic to the specified
host via the specified network interface.
|
void |
setBackgroundDataSetting(boolean allowBackgroundData)
Deprecated.
|
void |
setDataDependency(int networkType,
boolean met) |
void |
setGlobalProxy(ProxyProperties p) |
void |
setMobileDataEnabled(boolean enabled)
Sets the persisted value for enabling/disabling Mobile data.
|
void |
setNetworkPreference(int preference) |
boolean |
setRadio(int networkType,
boolean turnOn) |
boolean |
setRadios(boolean turnOn) |
int |
setUsbTethering(boolean enable) |
int |
startUsingNetworkFeature(int networkType,
String feature)
Tells the underlying networking system that the caller wants to
begin using the named feature.
|
int |
stopUsingNetworkFeature(int networkType,
String feature)
Tells the underlying networking system that the caller is finished
using the named feature.
|
int |
tether(String iface) |
int |
untether(String iface) |
boolean |
updateLockdownVpn() |
public static final String CONNECTIVITY_ACTION
true
if there are no connected networks at all.public static final String CONNECTIVITY_ACTION_IMMEDIATE
CONNECTIVITY_ACTION
broadcast, but sent without any
applicable Settings.Secure#CONNECTIVITY_CHANGE_DELAY
.@Deprecated public static final String EXTRA_NETWORK_INFO
NetworkInfo
can vary based on UID, applications
should always obtain network information through
getActiveNetworkInfo()
or
getAllNetworkInfo()
.NetworkInfo
object. Retrieve with
Intent.getParcelableExtra(String)
.EXTRA_NETWORK_TYPE
,
Constant Field Valuespublic static final String EXTRA_NETWORK_TYPE
CONNECTIVITY_ACTION
broadcast.
Can be used with getNetworkInfo(int)
to get NetworkInfo
state based on the calling application.public static final String EXTRA_IS_FAILOVER
Intent.getBooleanExtra(String,boolean)
.public static final String EXTRA_OTHER_NETWORK_INFO
NetworkInfo
object. This is supplied when
there is another network that it may be possible to connect to. Retrieve with
Intent.getParcelableExtra(String)
.public static final String EXTRA_NO_CONNECTIVITY
Intent.getBooleanExtra(String,boolean)
.public static final String EXTRA_REASON
Intent.getStringExtra(String)
.public static final String EXTRA_EXTRA_INFO
Intent.getStringExtra(String)
.public static final String EXTRA_INET_CONDITION
Intent.getIntExtra(String, int)
.
public static final String ACTION_DATA_ACTIVITY_CHANGE
public static final String EXTRA_DEVICE_TYPE
public static final String EXTRA_IS_ACTIVE
true
means
it is actively sending or receiving data and false
means it is idle.
@Deprecated public static final String ACTION_BACKGROUND_DATA_SETTING_CHANGED
Build.VERSION_CODES.ICE_CREAM_SANDWICH
, availability
of background data depends on several combined factors, and
this broadcast is no longer sent. Instead, when background
data is unavailable, getActiveNetworkInfo()
will now
appear disconnected. During first boot after a platform
upgrade, this broadcast will be sent once if
getBackgroundDataSetting()
was false
before
the upgrade.getBackgroundDataSetting()
to get the current value.
If an application uses the network in the background, it should listen
for this broadcast and stop using the background data if the value is
false
.
public static final String INET_CONDITION_ACTION
ConnectivityManager.EXTRA_INET_CONDITION
and
ConnectivityManager.EXTRA_NETWORK_INFO
to specify
the network and it's condition.public static final String ACTION_TETHER_STATE_CHANGED
public static final String EXTRA_AVAILABLE_TETHER
public static final String EXTRA_ACTIVE_TETHER
public static final String EXTRA_ERRORED_TETHER
public static final int TYPE_NONE
public static final int TYPE_MOBILE
public static final int TYPE_WIFI
public static final int TYPE_MOBILE_MMS
TYPE_MOBILE
but it may be different. This is used
by applications needing to talk to the carrier's Multimedia Messaging
Service servers. It may coexist with default data connections.public static final int TYPE_MOBILE_SUPL
TYPE_MOBILE
but it may be different. This is used
by applications needing to talk to the carrier's Secure User Plane
Location servers for help locating the device. It may coexist with
default data connections.public static final int TYPE_MOBILE_DUN
TYPE_MOBILE
but it may be different. This is used
by applicaitons performing a Dial Up Networking bridge so that
the carrier is aware of DUN traffic. It may coexist with default data
connections.public static final int TYPE_MOBILE_HIPRI
TYPE_MOBILE
but the routing setup is different.
Only requesting processes will have access to the Mobile DNS servers
and only IP's explicitly requested via requestRouteToHost(int, int)
will route over this interface if a default route exists.public static final int TYPE_WIMAX
public static final int TYPE_BLUETOOTH
public static final int TYPE_DUMMY
public static final int TYPE_ETHERNET
public static final int TYPE_MOBILE_FOTA
public static final int TYPE_MOBILE_IMS
public static final int TYPE_MOBILE_CBS
public static final int TYPE_WIFI_P2P
public static final int MAX_RADIO_TYPE
public static final int MAX_NETWORK_TYPE
public static final int DEFAULT_NETWORK_PREFERENCE
public static final int CONNECTIVITY_CHANGE_DELAY_DEFAULT
Settings.Global#CONNECTIVITY_CHANGE_DELAY
in
milliseconds.public static final int TETHER_ERROR_NO_ERROR
public static final int TETHER_ERROR_UNKNOWN_IFACE
public static final int TETHER_ERROR_SERVICE_UNAVAIL
public static final int TETHER_ERROR_UNSUPPORTED
public static final int TETHER_ERROR_UNAVAIL_IFACE
public static final int TETHER_ERROR_MASTER_ERROR
public static final int TETHER_ERROR_TETHER_IFACE_ERROR
public static final int TETHER_ERROR_UNTETHER_IFACE_ERROR
public static final int TETHER_ERROR_ENABLE_NAT_ERROR
public static final int TETHER_ERROR_DISABLE_NAT_ERROR
public static final int TETHER_ERROR_IFACE_CFG_ERROR
public static boolean isNetworkTypeValid(int networkType)
public static String getNetworkTypeName(int type)
public static boolean isNetworkTypeMobile(int networkType)
public void setNetworkPreference(int preference)
public int getNetworkPreference()
public NetworkInfo getActiveNetworkInfo()
NetworkInfo.isConnected()
before initiating network
traffic. This may return null
when no networks are available.
This method requires the caller to hold the permission
android.Manifest.permission#ACCESS_NETWORK_STATE
.
public NetworkInfo getActiveNetworkInfoForUid(int uid)
public NetworkInfo getNetworkInfo(int networkType)
public NetworkInfo[] getAllNetworkInfo()
public LinkProperties getActiveLinkProperties()
public LinkProperties getLinkProperties(int networkType)
public boolean setRadios(boolean turnOn)
public boolean setRadio(int networkType, boolean turnOn)
public int startUsingNetworkFeature(int networkType, String feature)
feature
is completely up to each networking implementation.
This method requires the caller to hold the permission
android.Manifest.permission#CHANGE_NETWORK_STATE
.
networkType
- specifies which network the request pertains tofeature
- the name of the feature to be used-1
always indicates failure.public int stopUsingNetworkFeature(int networkType, String feature)
feature
is completely up to each networking implementation.
This method requires the caller to hold the permission
android.Manifest.permission#CHANGE_NETWORK_STATE
.
networkType
- specifies which network the request pertains tofeature
- the name of the feature that is no longer needed-1
always indicates failure.public boolean requestRouteToHost(int networkType, int hostAddress)
This method requires the caller to hold the permission
android.Manifest.permission#CHANGE_NETWORK_STATE
.
networkType
- the type of the network over which traffic to the specified
host is to be routedhostAddress
- the IP address of the host to which the route is desiredtrue
on success, false
on failurepublic boolean requestRouteToHostAddress(int networkType, InetAddress hostAddress)
networkType
- the type of the network over which traffic to the specified
host is to be routedhostAddress
- the IP address of the host to which the route is desiredtrue
on success, false
on failure@Deprecated public boolean getBackgroundDataSetting()
Build.VERSION_CODES.ICE_CREAM_SANDWICH
, availability of
background data depends on several combined factors, and this method will
always return true
. Instead, when background data is unavailable,
getActiveNetworkInfo()
will now appear disconnected.
All applications that have background services that use the network
should listen to ACTION_BACKGROUND_DATA_SETTING_CHANGED
.
@Deprecated public void setBackgroundDataSetting(boolean allowBackgroundData)
allowBackgroundData
- Whether an application should use data while
it is in the background.getBackgroundDataSetting()
public NetworkQuotaInfo getActiveNetworkQuotaInfo()
null
if no
network is active. Quota status can change rapidly, so these values
shouldn't be cached.public boolean getMobileDataEnabled()
public void setMobileDataEnabled(boolean enabled)
enabled
- Whether the mobile data connection should be
used or not.public static ConnectivityManager from(Context context)
public String[] getTetherableIfaces()
public String[] getTetheredIfaces()
public String[] getTetheringErroredIfaces()
public int tether(String iface)
public int untether(String iface)
public boolean isTetheringSupported()
public String[] getTetherableUsbRegexs()
public String[] getTetherableWifiRegexs()
public String[] getTetherableBluetoothRegexs()
public int setUsbTethering(boolean enable)
public int getLastTetherError(String iface)
iface
- The name of the interface we're interested inpublic boolean requestNetworkTransitionWakelock(String forWhom)
forWhome
- The name of the network going down for logging purposestrue
on success, false
on failure
public void reportInetCondition(int networkType, int percentage)
networkType
- The type of network you want to report onpercentage
- The quality of the connection 0 is bad, 100 is good
public void setGlobalProxy(ProxyProperties p)
proxyProperties
- The definition for the new global http proxy
public ProxyProperties getGlobalProxy()
public ProxyProperties getProxy()
public void setDataDependency(int networkType, boolean met)
networkType
- The network who's dependence has changedmet
- Boolean - true if network use is ok, false if not
public boolean isNetworkSupported(int networkType)
networkType
- The nework type we'd like to checkpublic boolean isActiveNetworkMetered()
public boolean updateLockdownVpn()
public void captivePortalCheckComplete(NetworkInfo info)