public final class NetManager extends Object
This class provides interfaces to activate and query data networks and bind NICs to processes, as well as callback interfaces to listen for status changes.
| 限定符和类型 | 字段 | 说明 |
|---|---|---|
static int |
BACKGROUND_POLICY_ALLOWLISTED |
Indicates that only applications in the allowlist can use metered networks.
|
static int |
BACKGROUND_POLICY_DISABLE |
Indicates that applications can use metered networks.
|
static int |
BACKGROUND_POLICY_ENABLED |
Indicates that applications cannot use metered networks.
|
| 限定符和类型 | 方法 | 说明 |
|---|---|---|
boolean |
addDefaultNetStatusCallback(NetStatusCallback callback) |
Receives status change notifications of the default data network.
|
boolean |
addNetStatusCallback(NetSpecifier netSpecifier,
NetStatusCallback callback) |
Receives status change notifications of a specified network.
|
NetHandle[] |
getAllNets() |
Obtains the list of data networks that are activated.
|
NetHandle |
getAppNet() |
Obtains the
NetHandle bound to a process using setAppNet(ohos.net.NetHandle). |
int |
getBackgroundPolicy() |
Queries the background policy of the current application.
|
ConnectionProperties |
getConnectionProperties(NetHandle handle) |
Queries the connection properties of a network.
|
HttpProxy |
getDefaultHttpProxy() |
Obtains the default
HttpProxy proxy settings. |
NetHandle |
getDefaultNet() |
Obtains the data network that is activated by default.
|
static NetManager |
getInstance(Context context) |
Get the NetManager instance.
|
NetCapabilities |
getNetCapabilities(NetHandle handle) |
Obtains
NetCapabilities of a NetHandle object. |
boolean |
hasDefaultNet() |
Checks whether the default data network is activated.
|
boolean |
isDefaultNetMetered() |
Checks whether data traffic usage on the current network is metered.
|
boolean |
removeNetStatusCallback(NetStatusCallback callback) |
Cancels listening for network status changes.
|
boolean |
sendNetConnectState(NetHandle netHandle,
boolean isConnect) |
Reports the network state.
|
boolean |
setAppNet(NetHandle netHandle) |
Binds a process to
handle. |
boolean |
setupSpecificNet(NetSpecifier netSpecifier,
NetStatusCallback callback) |
Activates a data network matching
NetSpecifier. |
public static final int BACKGROUND_POLICY_ALLOWLISTED
public static final int BACKGROUND_POLICY_DISABLE
public static final int BACKGROUND_POLICY_ENABLED
public static NetManager getInstance(Context context)
context - application context.public boolean setupSpecificNet(NetSpecifier netSpecifier, NetStatusCallback callback)
NetSpecifier.
A network is automatically deactivated when the application exits.
It can also be manually deactivated by using removeNetStatusCallback(ohos.net.NetStatusCallback).
To call this method, you must have the ohos.permission.SET_NETWORK_INFO permission.
netSpecifier - Indicates the network specifier. See NetSpecifier.callback - Indicates the callback to notify the application of network status changes.
For details, see NetStatusCallback.true if the network is activated; returns false otherwise.public boolean addNetStatusCallback(NetSpecifier netSpecifier, NetStatusCallback callback)
removeNetStatusCallback(ohos.net.NetStatusCallback) can be used to cancel receiving status change notifications.
This method requires the ohos.permission.GET_NETWORK_INFO permission.
netSpecifier - Indicates the network specifier. See NetSpecifier.callback - Indicates the callback to notify the application of network status changes.
For details, see NetStatusCallback.true if notifications are received; returns false otherwise.public boolean removeNetStatusCallback(NetStatusCallback callback)
setupSpecificNet(ohos.net.NetSpecifier,ohos.net.NetStatusCallback),
network connection will be interrupted.callback - Indicates the callback to cancel. For details, see NetStatusCallback.true if listening for network status changes is canceled;
returns false otherwise.public boolean addDefaultNetStatusCallback(NetStatusCallback callback)
removeNetStatusCallback(ohos.net.NetStatusCallback) can be used to cancel receiving status change notifications.
This method requires the ohos.permission.GET_NETWORK_INFO permission.
callback - Indicates the callback to notify the application of network status changes.
For details, see NetStatusCallback.true if notifications are received; returns false otherwise.public NetHandle getDefaultNet()
To call this method, you must have the ohos.permission.GET_NETWORK_INFO permission.
NetHandle object;
returns null if the default network is not activated.public NetHandle[] getAllNets()
To invoke this method, you must have the ohos.permission.GET_NETWORK_INFO permission.
NetHandle object; returns null if no network is activated.public boolean hasDefaultNet()
true if the default data network is activated; returns false otherwise.public NetCapabilities getNetCapabilities(NetHandle handle)
NetCapabilities of a NetHandle object.
To invoke this method, you must have the ohos.permission.GET_NETWORK_INFO permission.
handle - Indicates the handle. See NetHandle.NetCapabilities; returns null if handle is invalid.public HttpProxy getDefaultHttpProxy()
HttpProxy proxy settings.
If a global proxy is set, the global proxy parameters are returned.
If the process is bound to a NetHandle using setAppNet(ohos.net.NetHandle),
the NetHandle proxy settings are returned.
In other cases, the default proxy settings of NetHandle are returned.
HttpProxy.public boolean setAppNet(NetHandle netHandle)
handle.
All the sockets created from the process will be bound to the handle,
and the resolution of all host names will be managed by the handle. You can unbind the process
from handle by setting nethanlde to null.
netHandle - Indicates the handle. For details, see NetHandle.public NetHandle getAppNet()
NetHandle bound to a process using setAppNet(ohos.net.NetHandle).public boolean isDefaultNetMetered()
To call this method, you must have the ohos.permission.GET_NETWORK_INFO permission.
true if data traffic usage on the current network is metered;
returns false otherwise.public ConnectionProperties getConnectionProperties(NetHandle handle)
This method requires the ohos.permission.GET_NETWORK_INFO permission.
handle - Indicates the network to be queried.ConnectionProperties object.public int getBackgroundPolicy()
BACKGROUND_POLICY_DISABLE.public boolean sendNetConnectState(NetHandle netHandle, boolean isConnect)
Permissions required: SystemPermission.GET_NETWORK_INFO
and SystemPermission.INTERNET
netHandle - Indicates the network whose state is to be reported.
The value nullindicates the current network.isConnect - Specifies whether the network is available.
The value true indicates that the network is available, and false indicates the opposite.true if the operation is successful; returns false otherwise.