2 * Copyright (C) 2008 The Android Open Source Project
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
19 import static com.android.internal.util.Preconditions.checkNotNull;
21 import android.annotation.SdkConstant;
22 import android.annotation.SdkConstant.SdkConstantType;
23 import android.content.Context;
24 import android.os.Binder;
25 import android.os.Build.VERSION_CODES;
26 import android.os.Messenger;
27 import android.os.RemoteException;
28 import android.provider.Settings;
30 import java.net.InetAddress;
33 * Class that answers queries about the state of network connectivity. It also
34 * notifies applications when network connectivity changes. Get an instance
35 * of this class by calling
36 * {@link android.content.Context#getSystemService(String) Context.getSystemService(Context.CONNECTIVITY_SERVICE)}.
38 * The primary responsibilities of this class are to:
40 * <li>Monitor network connections (Wi-Fi, GPRS, UMTS, etc.)</li>
41 * <li>Send broadcast intents when network connectivity changes</li>
42 * <li>Attempt to "fail over" to another network when connectivity to a network
44 * <li>Provide an API that allows applications to query the coarse-grained or fine-grained
45 * state of the available networks</li>
48 public class ConnectivityManager {
49 private static final String TAG = "ConnectivityManager";
52 * A change in network connectivity has occurred. A connection has either
53 * been established or lost. The NetworkInfo for the affected network is
54 * sent as an extra; it should be consulted to see what kind of
55 * connectivity event occurred.
57 * If this is a connection that was the result of failing over from a
58 * disconnected network, then the FAILOVER_CONNECTION boolean extra is
61 * For a loss of connectivity, if the connectivity manager is attempting
62 * to connect (or has already connected) to another network, the
63 * NetworkInfo for the new network is also passed as an extra. This lets
64 * any receivers of the broadcast know that they should not necessarily
65 * tell the user that no data traffic will be possible. Instead, the
66 * receiver should expect another broadcast soon, indicating either that
67 * the failover attempt succeeded (and so there is still overall data
68 * connectivity), or that the failover attempt failed, meaning that all
69 * connectivity has been lost.
71 * For a disconnect event, the boolean extra EXTRA_NO_CONNECTIVITY
72 * is set to {@code true} if there are no connected networks at all.
74 @SdkConstant(SdkConstantType.BROADCAST_INTENT_ACTION)
75 public static final String CONNECTIVITY_ACTION = "android.net.conn.CONNECTIVITY_CHANGE";
78 * Identical to {@link #CONNECTIVITY_ACTION} broadcast, but sent without any
79 * applicable {@link Settings.Secure#CONNECTIVITY_CHANGE_DELAY}.
83 @SdkConstant(SdkConstantType.BROADCAST_INTENT_ACTION)
84 public static final String CONNECTIVITY_ACTION_IMMEDIATE =
85 "android.net.conn.CONNECTIVITY_CHANGE_IMMEDIATE";
88 * The lookup key for a {@link NetworkInfo} object. Retrieve with
89 * {@link android.content.Intent#getParcelableExtra(String)}.
91 * @deprecated Since {@link NetworkInfo} can vary based on UID, applications
92 * should always obtain network information through
93 * {@link #getActiveNetworkInfo()} or
94 * {@link #getAllNetworkInfo()}.
95 * @see #EXTRA_NETWORK_TYPE
98 public static final String EXTRA_NETWORK_INFO = "networkInfo";
101 * Network type which triggered a {@link #CONNECTIVITY_ACTION} broadcast.
102 * Can be used with {@link #getNetworkInfo(int)} to get {@link NetworkInfo}
103 * state based on the calling application.
105 * @see android.content.Intent#getIntExtra(String, int)
107 public static final String EXTRA_NETWORK_TYPE = "networkType";
110 * The lookup key for a boolean that indicates whether a connect event
111 * is for a network to which the connectivity manager was failing over
112 * following a disconnect on another network.
113 * Retrieve it with {@link android.content.Intent#getBooleanExtra(String,boolean)}.
115 public static final String EXTRA_IS_FAILOVER = "isFailover";
117 * The lookup key for a {@link NetworkInfo} object. This is supplied when
118 * there is another network that it may be possible to connect to. Retrieve with
119 * {@link android.content.Intent#getParcelableExtra(String)}.
121 public static final String EXTRA_OTHER_NETWORK_INFO = "otherNetwork";
123 * The lookup key for a boolean that indicates whether there is a
124 * complete lack of connectivity, i.e., no network is available.
125 * Retrieve it with {@link android.content.Intent#getBooleanExtra(String,boolean)}.
127 public static final String EXTRA_NO_CONNECTIVITY = "noConnectivity";
129 * The lookup key for a string that indicates why an attempt to connect
130 * to a network failed. The string has no particular structure. It is
131 * intended to be used in notifications presented to users. Retrieve
132 * it with {@link android.content.Intent#getStringExtra(String)}.
134 public static final String EXTRA_REASON = "reason";
136 * The lookup key for a string that provides optionally supplied
137 * extra information about the network state. The information
138 * may be passed up from the lower networking layers, and its
139 * meaning may be specific to a particular network type. Retrieve
140 * it with {@link android.content.Intent#getStringExtra(String)}.
142 public static final String EXTRA_EXTRA_INFO = "extraInfo";
144 * The lookup key for an int that provides information about
145 * our connection to the internet at large. 0 indicates no connection,
146 * 100 indicates a great connection. Retrieve it with
147 * {@link android.content.Intent#getIntExtra(String, int)}.
150 public static final String EXTRA_INET_CONDITION = "inetCondition";
153 * Broadcast action to indicate the change of data activity status
154 * (idle or active) on a network in a recent period.
155 * The network becomes active when data transmission is started, or
156 * idle if there is no data transmission for a period of time.
159 @SdkConstant(SdkConstantType.BROADCAST_INTENT_ACTION)
160 public static final String ACTION_DATA_ACTIVITY_CHANGE = "android.net.conn.DATA_ACTIVITY_CHANGE";
162 * The lookup key for an enum that indicates the network device type on which this data activity
166 public static final String EXTRA_DEVICE_TYPE = "deviceType";
168 * The lookup key for a boolean that indicates the device is active or not. {@code true} means
169 * it is actively sending or receiving data and {@code false} means it is idle.
172 public static final String EXTRA_IS_ACTIVE = "isActive";
175 * Broadcast Action: The setting for background data usage has changed
176 * values. Use {@link #getBackgroundDataSetting()} to get the current value.
178 * If an application uses the network in the background, it should listen
179 * for this broadcast and stop using the background data if the value is
183 * @deprecated As of {@link VERSION_CODES#ICE_CREAM_SANDWICH}, availability
184 * of background data depends on several combined factors, and
185 * this broadcast is no longer sent. Instead, when background
186 * data is unavailable, {@link #getActiveNetworkInfo()} will now
187 * appear disconnected. During first boot after a platform
188 * upgrade, this broadcast will be sent once if
189 * {@link #getBackgroundDataSetting()} was {@code false} before
192 @SdkConstant(SdkConstantType.BROADCAST_INTENT_ACTION)
194 public static final String ACTION_BACKGROUND_DATA_SETTING_CHANGED =
195 "android.net.conn.BACKGROUND_DATA_SETTING_CHANGED";
198 * Broadcast Action: The network connection may not be good
199 * uses {@code ConnectivityManager.EXTRA_INET_CONDITION} and
200 * {@code ConnectivityManager.EXTRA_NETWORK_INFO} to specify
201 * the network and it's condition.
204 @SdkConstant(SdkConstantType.BROADCAST_INTENT_ACTION)
205 public static final String INET_CONDITION_ACTION =
206 "android.net.conn.INET_CONDITION_ACTION";
209 * Broadcast Action: A tetherable connection has come or gone.
210 * Uses {@code ConnectivityManager.EXTRA_AVAILABLE_TETHER},
211 * {@code ConnectivityManager.EXTRA_ACTIVE_TETHER} and
212 * {@code ConnectivityManager.EXTRA_ERRORED_TETHER} to indicate
213 * the current state of tethering. Each include a list of
214 * interface names in that state (may be empty).
217 @SdkConstant(SdkConstantType.BROADCAST_INTENT_ACTION)
218 public static final String ACTION_TETHER_STATE_CHANGED =
219 "android.net.conn.TETHER_STATE_CHANGED";
223 * gives a String[] listing all the interfaces configured for
224 * tethering and currently available for tethering.
226 public static final String EXTRA_AVAILABLE_TETHER = "availableArray";
230 * gives a String[] listing all the interfaces currently tethered
231 * (ie, has dhcp support and packets potentially forwarded/NATed)
233 public static final String EXTRA_ACTIVE_TETHER = "activeArray";
237 * gives a String[] listing all the interfaces we tried to tether and
238 * failed. Use {@link #getLastTetherError} to find the error code
239 * for any interfaces listed here.
241 public static final String EXTRA_ERRORED_TETHER = "erroredArray";
244 * Broadcast Action: The captive portal tracker has finished its test.
245 * Sent only while running Setup Wizard, in lieu of showing a user
249 @SdkConstant(SdkConstantType.BROADCAST_INTENT_ACTION)
250 public static final String ACTION_CAPTIVE_PORTAL_TEST_COMPLETED =
251 "android.net.conn.CAPTIVE_PORTAL_TEST_COMPLETED";
253 * The lookup key for a boolean that indicates whether a captive portal was detected.
254 * Retrieve it with {@link android.content.Intent#getBooleanExtra(String,boolean)}.
257 public static final String EXTRA_IS_CAPTIVE_PORTAL = "captivePortal";
260 * The absence of a connection type.
263 public static final int TYPE_NONE = -1;
266 * The Mobile data connection. When active, all data traffic
267 * will use this network type's interface by default
268 * (it has a default route)
270 public static final int TYPE_MOBILE = 0;
272 * The WIFI data connection. When active, all data traffic
273 * will use this network type's interface by default
274 * (it has a default route).
276 public static final int TYPE_WIFI = 1;
278 * An MMS-specific Mobile data connection. This network type may use the
279 * same network interface as {@link #TYPE_MOBILE} or it may use a different
280 * one. This is used by applications needing to talk to the carrier's
281 * Multimedia Messaging Service servers.
283 public static final int TYPE_MOBILE_MMS = 2;
285 * A SUPL-specific Mobile data connection. This network type may use the
286 * same network interface as {@link #TYPE_MOBILE} or it may use a different
287 * one. This is used by applications needing to talk to the carrier's
288 * Secure User Plane Location servers for help locating the device.
290 public static final int TYPE_MOBILE_SUPL = 3;
292 * A DUN-specific Mobile data connection. This network type may use the
293 * same network interface as {@link #TYPE_MOBILE} or it may use a different
294 * one. This is sometimes by the system when setting up an upstream connection
295 * for tethering so that the carrier is aware of DUN traffic.
297 public static final int TYPE_MOBILE_DUN = 4;
299 * A High Priority Mobile data connection. This network type uses the
300 * same network interface as {@link #TYPE_MOBILE} but the routing setup
301 * is different. Only requesting processes will have access to the
302 * Mobile DNS servers and only IP's explicitly requested via {@link #requestRouteToHost}
303 * will route over this interface if no default route exists.
305 public static final int TYPE_MOBILE_HIPRI = 5;
307 * The WiMAX data connection. When active, all data traffic
308 * will use this network type's interface by default
309 * (it has a default route).
311 public static final int TYPE_WIMAX = 6;
314 * The Bluetooth data connection. When active, all data traffic
315 * will use this network type's interface by default
316 * (it has a default route).
318 public static final int TYPE_BLUETOOTH = 7;
321 * Dummy data connection. This should not be used on shipping devices.
323 public static final int TYPE_DUMMY = 8;
326 * The Ethernet data connection. When active, all data traffic
327 * will use this network type's interface by default
328 * (it has a default route).
330 public static final int TYPE_ETHERNET = 9;
333 * Over the air Administration.
336 public static final int TYPE_MOBILE_FOTA = 10;
339 * IP Multimedia Subsystem.
342 public static final int TYPE_MOBILE_IMS = 11;
345 * Carrier Branded Services.
348 public static final int TYPE_MOBILE_CBS = 12;
351 * A Wi-Fi p2p connection. Only requesting processes will have access to
352 * the peers connected.
355 public static final int TYPE_WIFI_P2P = 13;
358 public static final int MAX_RADIO_TYPE = TYPE_WIFI_P2P;
361 public static final int MAX_NETWORK_TYPE = TYPE_WIFI_P2P;
364 * If you want to set the default network preference,you can directly
365 * change the networkAttributes array in framework's config.xml.
367 * @deprecated Since we support so many more networks now, the single
368 * network default network preference can't really express
369 * the hierarchy. Instead, the default is defined by the
370 * networkAttributes in config.xml. You can determine
371 * the current value by calling {@link #getNetworkPreference()}
375 public static final int DEFAULT_NETWORK_PREFERENCE = TYPE_WIFI;
378 * Default value for {@link Settings.Global#CONNECTIVITY_CHANGE_DELAY} in
379 * milliseconds. This was introduced because IPv6 routes seem to take a
380 * moment to settle - trying network activity before the routes are adjusted
381 * can lead to packets using the wrong interface or having the wrong IP address.
382 * This delay is a bit crude, but in the future hopefully we will have kernel
383 * notifications letting us know when it's safe to use the new network.
387 public static final int CONNECTIVITY_CHANGE_DELAY_DEFAULT = 3000;
389 private final IConnectivityManager mService;
392 * Tests if a given integer represents a valid network type.
393 * @param networkType the type to be tested
394 * @return a boolean. {@code true} if the type is valid, else {@code false}
396 public static boolean isNetworkTypeValid(int networkType) {
397 return networkType >= 0 && networkType <= MAX_NETWORK_TYPE;
401 * Returns a non-localized string representing a given network type.
402 * ONLY used for debugging output.
403 * @param type the type needing naming
404 * @return a String for the given type, or a string version of the type ("87")
405 * if no name is known.
408 public static String getNetworkTypeName(int type) {
414 case TYPE_MOBILE_MMS:
416 case TYPE_MOBILE_SUPL:
417 return "MOBILE_SUPL";
418 case TYPE_MOBILE_DUN:
420 case TYPE_MOBILE_HIPRI:
421 return "MOBILE_HIPRI";
430 case TYPE_MOBILE_FOTA:
431 return "MOBILE_FOTA";
432 case TYPE_MOBILE_IMS:
434 case TYPE_MOBILE_CBS:
439 return Integer.toString(type);
444 * Checks if a given type uses the cellular data connection.
445 * This should be replaced in the future by a network property.
446 * @param networkType the type to check
447 * @return a boolean - {@code true} if uses cellular network, else {@code false}
450 public static boolean isNetworkTypeMobile(int networkType) {
451 switch (networkType) {
453 case TYPE_MOBILE_MMS:
454 case TYPE_MOBILE_SUPL:
455 case TYPE_MOBILE_DUN:
456 case TYPE_MOBILE_HIPRI:
457 case TYPE_MOBILE_FOTA:
458 case TYPE_MOBILE_IMS:
459 case TYPE_MOBILE_CBS:
467 * Specifies the preferred network type. When the device has more
468 * than one type available the preferred network type will be used.
469 * Note that this made sense when we only had 2 network types,
470 * but with more and more default networks we need an array to list
471 * their ordering. This will be deprecated soon.
473 * @param preference the network type to prefer over all others. It is
474 * unspecified what happens to the old preferred network in the
477 public void setNetworkPreference(int preference) {
479 mService.setNetworkPreference(preference);
480 } catch (RemoteException e) {
485 * Retrieves the current preferred network type.
486 * Note that this made sense when we only had 2 network types,
487 * but with more and more default networks we need an array to list
488 * their ordering. This will be deprecated soon.
490 * @return an integer representing the preferred network type
492 * <p>This method requires the caller to hold the permission
493 * {@link android.Manifest.permission#ACCESS_NETWORK_STATE}.
495 public int getNetworkPreference() {
497 return mService.getNetworkPreference();
498 } catch (RemoteException e) {
504 * Returns details about the currently active default data network. When
505 * connected, this network is the default route for outgoing connections.
506 * You should always check {@link NetworkInfo#isConnected()} before initiating
507 * network traffic. This may return {@code null} when there is no default
510 * @return a {@link NetworkInfo} object for the current default network
511 * or {@code null} if no network default network is currently active
513 * <p>This method requires the call to hold the permission
514 * {@link android.Manifest.permission#ACCESS_NETWORK_STATE}.
516 public NetworkInfo getActiveNetworkInfo() {
518 return mService.getActiveNetworkInfo();
519 } catch (RemoteException e) {
525 * Returns details about the currently active default data network
526 * for a given uid. This is for internal use only to avoid spying
529 * @return a {@link NetworkInfo} object for the current default network
530 * for the given uid or {@code null} if no default network is
531 * available for the specified uid.
533 * <p>This method requires the caller to hold the permission
534 * {@link android.Manifest.permission#CONNECTIVITY_INTERNAL}
537 public NetworkInfo getActiveNetworkInfoForUid(int uid) {
539 return mService.getActiveNetworkInfoForUid(uid);
540 } catch (RemoteException e) {
546 * Returns connection status information about a particular
549 * @param networkType integer specifying which networkType in
550 * which you're interested.
551 * @return a {@link NetworkInfo} object for the requested
552 * network type or {@code null} if the type is not
553 * supported by the device.
555 * <p>This method requires the call to hold the permission
556 * {@link android.Manifest.permission#ACCESS_NETWORK_STATE}.
558 public NetworkInfo getNetworkInfo(int networkType) {
560 return mService.getNetworkInfo(networkType);
561 } catch (RemoteException e) {
567 * Returns connection status information about all network
568 * types supported by the device.
570 * @return an array of {@link NetworkInfo} objects. Check each
571 * {@link NetworkInfo#getType} for which type each applies.
573 * <p>This method requires the call to hold the permission
574 * {@link android.Manifest.permission#ACCESS_NETWORK_STATE}.
576 public NetworkInfo[] getAllNetworkInfo() {
578 return mService.getAllNetworkInfo();
579 } catch (RemoteException e) {
585 * Returns the IP information for the current default network.
587 * @return a {@link LinkProperties} object describing the IP info
588 * for the current default network, or {@code null} if there
589 * is no current default network.
591 * <p>This method requires the call to hold the permission
592 * {@link android.Manifest.permission#ACCESS_NETWORK_STATE}.
595 public LinkProperties getActiveLinkProperties() {
597 return mService.getActiveLinkProperties();
598 } catch (RemoteException e) {
604 * Returns the IP information for a given network type.
606 * @param networkType the network type of interest.
607 * @return a {@link LinkProperties} object describing the IP info
608 * for the given networkType, or {@code null} if there is
609 * no current default network.
611 * <p>This method requires the call to hold the permission
612 * {@link android.Manifest.permission#ACCESS_NETWORK_STATE}.
615 public LinkProperties getLinkProperties(int networkType) {
617 return mService.getLinkProperties(networkType);
618 } catch (RemoteException e) {
624 * Tells each network type to set its radio power state as directed.
626 * @param turnOn a boolean, {@code true} to turn the radios on,
627 * {@code false} to turn them off.
628 * @return a boolean, {@code true} indicating success. All network types
629 * will be tried, even if some fail.
631 * <p>This method requires the call to hold the permission
632 * {@link android.Manifest.permission#CHANGE_NETWORK_STATE}.
635 public boolean setRadios(boolean turnOn) {
637 return mService.setRadios(turnOn);
638 } catch (RemoteException e) {
644 * Tells a given networkType to set its radio power state as directed.
646 * @param networkType the int networkType of interest.
647 * @param turnOn a boolean, {@code true} to turn the radio on,
648 * {@code} false to turn it off.
649 * @return a boolean, {@code true} indicating success.
651 * <p>This method requires the call to hold the permission
652 * {@link android.Manifest.permission#CHANGE_NETWORK_STATE}.
655 public boolean setRadio(int networkType, boolean turnOn) {
657 return mService.setRadio(networkType, turnOn);
658 } catch (RemoteException e) {
664 * Tells the underlying networking system that the caller wants to
665 * begin using the named feature. The interpretation of {@code feature}
666 * is completely up to each networking implementation.
667 * <p>This method requires the caller to hold the permission
668 * {@link android.Manifest.permission#CHANGE_NETWORK_STATE}.
669 * @param networkType specifies which network the request pertains to
670 * @param feature the name of the feature to be used
671 * @return an integer value representing the outcome of the request.
672 * The interpretation of this value is specific to each networking
673 * implementation+feature combination, except that the value {@code -1}
674 * always indicates failure.
676 public int startUsingNetworkFeature(int networkType, String feature) {
678 return mService.startUsingNetworkFeature(networkType, feature,
680 } catch (RemoteException e) {
686 * Tells the underlying networking system that the caller is finished
687 * using the named feature. The interpretation of {@code feature}
688 * is completely up to each networking implementation.
689 * <p>This method requires the caller to hold the permission
690 * {@link android.Manifest.permission#CHANGE_NETWORK_STATE}.
691 * @param networkType specifies which network the request pertains to
692 * @param feature the name of the feature that is no longer needed
693 * @return an integer value representing the outcome of the request.
694 * The interpretation of this value is specific to each networking
695 * implementation+feature combination, except that the value {@code -1}
696 * always indicates failure.
698 public int stopUsingNetworkFeature(int networkType, String feature) {
700 return mService.stopUsingNetworkFeature(networkType, feature);
701 } catch (RemoteException e) {
707 * Ensure that a network route exists to deliver traffic to the specified
708 * host via the specified network interface. An attempt to add a route that
709 * already exists is ignored, but treated as successful.
710 * <p>This method requires the caller to hold the permission
711 * {@link android.Manifest.permission#CHANGE_NETWORK_STATE}.
712 * @param networkType the type of the network over which traffic to the specified
713 * host is to be routed
714 * @param hostAddress the IP address of the host to which the route is desired
715 * @return {@code true} on success, {@code false} on failure
717 public boolean requestRouteToHost(int networkType, int hostAddress) {
718 InetAddress inetAddress = NetworkUtils.intToInetAddress(hostAddress);
720 if (inetAddress == null) {
724 return requestRouteToHostAddress(networkType, inetAddress);
728 * Ensure that a network route exists to deliver traffic to the specified
729 * host via the specified network interface. An attempt to add a route that
730 * already exists is ignored, but treated as successful.
731 * @param networkType the type of the network over which traffic to the specified
732 * host is to be routed
733 * @param hostAddress the IP address of the host to which the route is desired
734 * @return {@code true} on success, {@code false} on failure
737 public boolean requestRouteToHostAddress(int networkType, InetAddress hostAddress) {
738 byte[] address = hostAddress.getAddress();
740 return mService.requestRouteToHostAddress(networkType, address);
741 } catch (RemoteException e) {
747 * Returns the value of the setting for background data usage. If false,
748 * applications should not use the network if the application is not in the
749 * foreground. Developers should respect this setting, and check the value
750 * of this before performing any background data operations.
752 * All applications that have background services that use the network
753 * should listen to {@link #ACTION_BACKGROUND_DATA_SETTING_CHANGED}.
755 * @deprecated As of {@link VERSION_CODES#ICE_CREAM_SANDWICH}, availability of
756 * background data depends on several combined factors, and this method will
757 * always return {@code true}. Instead, when background data is unavailable,
758 * {@link #getActiveNetworkInfo()} will now appear disconnected.
760 * @return Whether background data usage is allowed.
763 public boolean getBackgroundDataSetting() {
764 // assume that background data is allowed; final authority is
765 // NetworkInfo which may be blocked.
770 * Sets the value of the setting for background data usage.
772 * @param allowBackgroundData Whether an application should use data while
773 * it is in the background.
775 * @attr ref android.Manifest.permission#CHANGE_BACKGROUND_DATA_SETTING
776 * @see #getBackgroundDataSetting()
780 public void setBackgroundDataSetting(boolean allowBackgroundData) {
785 * Return quota status for the current active network, or {@code null} if no
786 * network is active. Quota status can change rapidly, so these values
787 * shouldn't be cached.
789 * <p>This method requires the call to hold the permission
790 * {@link android.Manifest.permission#ACCESS_NETWORK_STATE}.
794 public NetworkQuotaInfo getActiveNetworkQuotaInfo() {
796 return mService.getActiveNetworkQuotaInfo();
797 } catch (RemoteException e) {
803 * Gets the value of the setting for enabling Mobile data.
805 * @return Whether mobile data is enabled.
807 * <p>This method requires the call to hold the permission
808 * {@link android.Manifest.permission#ACCESS_NETWORK_STATE}.
811 public boolean getMobileDataEnabled() {
813 return mService.getMobileDataEnabled();
814 } catch (RemoteException e) {
820 * Sets the persisted value for enabling/disabling Mobile data.
822 * @param enabled Whether the user wants the mobile data connection used
826 public void setMobileDataEnabled(boolean enabled) {
828 mService.setMobileDataEnabled(enabled);
829 } catch (RemoteException e) {
836 public ConnectivityManager(IConnectivityManager service) {
837 mService = checkNotNull(service, "missing IConnectivityManager");
841 public static ConnectivityManager from(Context context) {
842 return (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
846 * Get the set of tetherable, available interfaces. This list is limited by
847 * device configuration and current interface existence.
849 * @return an array of 0 or more Strings of tetherable interface names.
851 * <p>This method requires the call to hold the permission
852 * {@link android.Manifest.permission#ACCESS_NETWORK_STATE}.
855 public String[] getTetherableIfaces() {
857 return mService.getTetherableIfaces();
858 } catch (RemoteException e) {
859 return new String[0];
864 * Get the set of tethered interfaces.
866 * @return an array of 0 or more String of currently tethered interface names.
868 * <p>This method requires the call to hold the permission
869 * {@link android.Manifest.permission#ACCESS_NETWORK_STATE}.
872 public String[] getTetheredIfaces() {
874 return mService.getTetheredIfaces();
875 } catch (RemoteException e) {
876 return new String[0];
881 * Get the set of interface names which attempted to tether but
882 * failed. Re-attempting to tether may cause them to reset to the Tethered
883 * state. Alternatively, causing the interface to be destroyed and recreated
884 * may cause them to reset to the available state.
885 * {@link ConnectivityManager#getLastTetherError} can be used to get more
886 * information on the cause of the errors.
888 * @return an array of 0 or more String indicating the interface names
889 * which failed to tether.
891 * <p>This method requires the call to hold the permission
892 * {@link android.Manifest.permission#ACCESS_NETWORK_STATE}.
895 public String[] getTetheringErroredIfaces() {
897 return mService.getTetheringErroredIfaces();
898 } catch (RemoteException e) {
899 return new String[0];
904 * Attempt to tether the named interface. This will setup a dhcp server
905 * on the interface, forward and NAT IP packets and forward DNS requests
906 * to the best active upstream network interface. Note that if no upstream
907 * IP network interface is available, dhcp will still run and traffic will be
908 * allowed between the tethered devices and this device, though upstream net
909 * access will of course fail until an upstream network interface becomes
912 * @param iface the interface name to tether.
913 * @return error a {@code TETHER_ERROR} value indicating success or failure type
915 * <p>This method requires the call to hold the permission
916 * {@link android.Manifest.permission#CHANGE_NETWORK_STATE}.
919 public int tether(String iface) {
921 return mService.tether(iface);
922 } catch (RemoteException e) {
923 return TETHER_ERROR_SERVICE_UNAVAIL;
928 * Stop tethering the named interface.
930 * @param iface the interface name to untether.
931 * @return error a {@code TETHER_ERROR} value indicating success or failure type
933 * <p>This method requires the call to hold the permission
934 * {@link android.Manifest.permission#CHANGE_NETWORK_STATE}.
937 public int untether(String iface) {
939 return mService.untether(iface);
940 } catch (RemoteException e) {
941 return TETHER_ERROR_SERVICE_UNAVAIL;
946 * Check if the device allows for tethering. It may be disabled via
947 * {@code ro.tether.denied} system property, {@link Settings#TETHER_SUPPORTED} or
948 * due to device configuration.
950 * @return a boolean - {@code true} indicating Tethering is supported.
952 * <p>This method requires the call to hold the permission
953 * {@link android.Manifest.permission#ACCESS_NETWORK_STATE}.
956 public boolean isTetheringSupported() {
958 return mService.isTetheringSupported();
959 } catch (RemoteException e) {
965 * Get the list of regular expressions that define any tetherable
966 * USB network interfaces. If USB tethering is not supported by the
967 * device, this list should be empty.
969 * @return an array of 0 or more regular expression Strings defining
970 * what interfaces are considered tetherable usb interfaces.
972 * <p>This method requires the call to hold the permission
973 * {@link android.Manifest.permission#ACCESS_NETWORK_STATE}.
976 public String[] getTetherableUsbRegexs() {
978 return mService.getTetherableUsbRegexs();
979 } catch (RemoteException e) {
980 return new String[0];
985 * Get the list of regular expressions that define any tetherable
986 * Wifi network interfaces. If Wifi tethering is not supported by the
987 * device, this list should be empty.
989 * @return an array of 0 or more regular expression Strings defining
990 * what interfaces are considered tetherable wifi interfaces.
992 * <p>This method requires the call to hold the permission
993 * {@link android.Manifest.permission#ACCESS_NETWORK_STATE}.
996 public String[] getTetherableWifiRegexs() {
998 return mService.getTetherableWifiRegexs();
999 } catch (RemoteException e) {
1000 return new String[0];
1005 * Get the list of regular expressions that define any tetherable
1006 * Bluetooth network interfaces. If Bluetooth tethering is not supported by the
1007 * device, this list should be empty.
1009 * @return an array of 0 or more regular expression Strings defining
1010 * what interfaces are considered tetherable bluetooth interfaces.
1012 * <p>This method requires the call to hold the permission
1013 * {@link android.Manifest.permission#ACCESS_NETWORK_STATE}.
1016 public String[] getTetherableBluetoothRegexs() {
1018 return mService.getTetherableBluetoothRegexs();
1019 } catch (RemoteException e) {
1020 return new String[0];
1025 * Attempt to both alter the mode of USB and Tethering of USB. A
1026 * utility method to deal with some of the complexity of USB - will
1027 * attempt to switch to Rndis and subsequently tether the resulting
1028 * interface on {@code true} or turn off tethering and switch off
1029 * Rndis on {@code false}.
1031 * @param enable a boolean - {@code true} to enable tethering
1032 * @return error a {@code TETHER_ERROR} value indicating success or failure type
1034 * <p>This method requires the call to hold the permission
1035 * {@link android.Manifest.permission#CHANGE_NETWORK_STATE}.
1038 public int setUsbTethering(boolean enable) {
1040 return mService.setUsbTethering(enable);
1041 } catch (RemoteException e) {
1042 return TETHER_ERROR_SERVICE_UNAVAIL;
1047 public static final int TETHER_ERROR_NO_ERROR = 0;
1049 public static final int TETHER_ERROR_UNKNOWN_IFACE = 1;
1051 public static final int TETHER_ERROR_SERVICE_UNAVAIL = 2;
1053 public static final int TETHER_ERROR_UNSUPPORTED = 3;
1055 public static final int TETHER_ERROR_UNAVAIL_IFACE = 4;
1057 public static final int TETHER_ERROR_MASTER_ERROR = 5;
1059 public static final int TETHER_ERROR_TETHER_IFACE_ERROR = 6;
1061 public static final int TETHER_ERROR_UNTETHER_IFACE_ERROR = 7;
1063 public static final int TETHER_ERROR_ENABLE_NAT_ERROR = 8;
1065 public static final int TETHER_ERROR_DISABLE_NAT_ERROR = 9;
1067 public static final int TETHER_ERROR_IFACE_CFG_ERROR = 10;
1070 * Get a more detailed error code after a Tethering or Untethering
1071 * request asynchronously failed.
1073 * @param iface The name of the interface of interest
1074 * @return error The error code of the last error tethering or untethering the named
1077 * <p>This method requires the call to hold the permission
1078 * {@link android.Manifest.permission#ACCESS_NETWORK_STATE}.
1081 public int getLastTetherError(String iface) {
1083 return mService.getLastTetherError(iface);
1084 } catch (RemoteException e) {
1085 return TETHER_ERROR_SERVICE_UNAVAIL;
1090 * Try to ensure the device stays awake until we connect with the next network.
1091 * Actually just holds a wakelock for a number of seconds while we try to connect
1092 * to any default networks. This will expire if the timeout passes or if we connect
1093 * to a default after this is called. For internal use only.
1095 * @param forWhom the name of the network going down for logging purposes
1096 * @return {@code true} on success, {@code false} on failure
1098 * <p>This method requires the call to hold the permission
1099 * {@link android.Manifest.permission#CONNECTIVITY_INTERNAL}.
1102 public boolean requestNetworkTransitionWakelock(String forWhom) {
1104 mService.requestNetworkTransitionWakelock(forWhom);
1106 } catch (RemoteException e) {
1112 * Report network connectivity status. This is currently used only
1113 * to alter status bar UI.
1115 * @param networkType The type of network you want to report on
1116 * @param percentage The quality of the connection 0 is bad, 100 is good
1118 * <p>This method requires the call to hold the permission
1119 * {@link android.Manifest.permission#STATUS_BAR}.
1122 public void reportInetCondition(int networkType, int percentage) {
1124 mService.reportInetCondition(networkType, percentage);
1125 } catch (RemoteException e) {
1130 * Set a network-independent global http proxy. This is not normally what you want
1131 * for typical HTTP proxies - they are general network dependent. However if you're
1132 * doing something unusual like general internal filtering this may be useful. On
1133 * a private network where the proxy is not accessible, you may break HTTP using this.
1135 * @param proxyProperties The a {@link ProxyProperites} object defining the new global
1136 * HTTP proxy. A {@code null} value will clear the global HTTP proxy.
1138 * <p>This method requires the call to hold the permission
1139 * {@link android.Manifest.permission#CONNECTIVITY_INTERNAL}.
1142 public void setGlobalProxy(ProxyProperties p) {
1144 mService.setGlobalProxy(p);
1145 } catch (RemoteException e) {
1150 * Retrieve any network-independent global HTTP proxy.
1152 * @return {@link ProxyProperties} for the current global HTTP proxy or {@code null}
1153 * if no global HTTP proxy is set.
1155 * <p>This method requires the call to hold the permission
1156 * {@link android.Manifest.permission#ACCESS_NETWORK_STATE}.
1159 public ProxyProperties getGlobalProxy() {
1161 return mService.getGlobalProxy();
1162 } catch (RemoteException e) {
1168 * Get the HTTP proxy settings for the current default network. Note that
1169 * if a global proxy is set, it will override any per-network setting.
1171 * @return the {@link ProxyProperties} for the current HTTP proxy, or {@code null} if no
1172 * HTTP proxy is active.
1174 * <p>This method requires the call to hold the permission
1175 * {@link android.Manifest.permission#ACCESS_NETWORK_STATE}.
1178 public ProxyProperties getProxy() {
1180 return mService.getProxy();
1181 } catch (RemoteException e) {
1187 * Sets a secondary requirement bit for the given networkType.
1188 * This requirement bit is generally under the control of the carrier
1189 * or its agents and is not directly controlled by the user.
1191 * @param networkType The network who's dependence has changed
1192 * @param met Boolean - true if network use is OK, false if not
1194 * <p>This method requires the call to hold the permission
1195 * {@link android.Manifest.permission#CONNECTIVITY_INTERNAL}.
1198 public void setDataDependency(int networkType, boolean met) {
1200 mService.setDataDependency(networkType, met);
1201 } catch (RemoteException e) {
1206 * Returns true if the hardware supports the given network type
1207 * else it returns false. This doesn't indicate we have coverage
1208 * or are authorized onto a network, just whether or not the
1209 * hardware supports it. For example a GSM phone without a SIM
1210 * should still return {@code true} for mobile data, but a wifi only
1211 * tablet would return {@code false}.
1213 * @param networkType The network type we'd like to check
1214 * @return {@code true} if supported, else {@code false}
1216 * <p>This method requires the call to hold the permission
1217 * {@link android.Manifest.permission#ACCESS_NETWORK_STATE}.
1220 public boolean isNetworkSupported(int networkType) {
1222 return mService.isNetworkSupported(networkType);
1223 } catch (RemoteException e) {}
1228 * Returns if the currently active data network is metered. A network is
1229 * classified as metered when the user is sensitive to heavy data usage on
1230 * that connection due to monetary costs, data limitations or
1231 * battery/performance issues. You should check this before doing large
1232 * data transfers, and warn the user or delay the operation until another
1233 * network is available.
1235 * @return {@code true} if large transfers should be avoided, otherwise
1238 * <p>This method requires the call to hold the permission
1239 * {@link android.Manifest.permission#ACCESS_NETWORK_STATE}.
1241 public boolean isActiveNetworkMetered() {
1243 return mService.isActiveNetworkMetered();
1244 } catch (RemoteException e) {
1250 * If the LockdownVpn mechanism is enabled, updates the vpn
1251 * with a reload of its profile.
1253 * @return a boolean with {@code} indicating success
1255 * <p>This method can only be called by the system UID
1258 public boolean updateLockdownVpn() {
1260 return mService.updateLockdownVpn();
1261 } catch (RemoteException e) {
1267 * Signal that the captive portal check on the indicated network
1268 * is complete and we can turn the network on for general use.
1270 * @param info the {@link NetworkInfo} object for the networkType
1273 * <p>This method requires the call to hold the permission
1274 * {@link android.Manifest.permission#CONNECTIVITY_INTERNAL}.
1277 public void captivePortalCheckComplete(NetworkInfo info) {
1279 mService.captivePortalCheckComplete(info);
1280 } catch (RemoteException e) {
1285 * Supply the backend messenger for a network tracker
1287 * @param type NetworkType to set
1288 * @param messenger {@link Messenger}
1291 public void supplyMessenger(int networkType, Messenger messenger) {
1293 mService.supplyMessenger(networkType, messenger);
1294 } catch (RemoteException e) {