harmony 鸿蒙@ohos.net.policy (Network Policy Management)

2022-10-28 浏览 (644)

@ohos.net.policy (Network Policy Management)

The policy module provides APIs for managing network policies, through which you can control and manage the data volume used.

NOTE

The initial APIs of this module are supported since API version 10. Newly added APIs will be marked with a superscript to indicate their earliest API version.

Modules to Import

import policy from '@ohos.net.policy';

policy.setBackgroundAllowed10+

setBackgroundAllowed(isAllowed: boolean, callback: AsyncCallback<void>): void

Specifies whether background applications are allowed to access the network. This API uses an asynchronous callback to return the result.

System API: This is a system API.

Required permission: ohos.permission.MANAGE_NET_STRATEGY

System capability: SystemCapability.Communication.NetManager.Core

Parameters

NameTypeMandatoryDescription
isAllowedbooleanYesWhether background applications are allowed to use mobile data.
callbackAsyncCallback<void>YesCallback used to return the result. If the operation is successful, err is undefined. Otherwise, err is an error object.

Error codes

IDError Message
201Permission denied.
202Non-system applications use system APIs.
401Parameter error.
2100001Invalid parameter value.
2100002Operation failed. Cannot connect to service.
2100003System internal error.

Example

import { BusinessError } from '@ohos.base';
policy.setBackgroundAllowed(true, (error: BusinessError) => {
  console.log(JSON.stringify(error));
});

policy.setBackgroundAllowed10+

setBackgroundAllowed(isAllowed: boolean): Promise<void>

Specifies whether background applications are allowed to access the network. This API uses a promise to return the result.

System API: This is a system API.

Required permission: ohos.permission.MANAGE_NET_STRATEGY

System capability: SystemCapability.Communication.NetManager.Core

Parameters

NameTypeMandatoryDescription
isAllowedbooleanYesWhether background applications are allowed to use mobile data.

Error codes

IDError Message
201Permission denied.
202Non-system applications use system APIs.
401Parameter error.
2100001Invalid parameter value.
2100002Operation failed. Cannot connect to service.
2100003System internal error.

Return value

TypeDescription
Promise<void>Promise used to return the result. If the operation is successful, no value is returned. If the operation fails, an error message is returned.

Example

import { BusinessError } from '@ohos.base';
policy.setBackgroundAllowed(true).then(() => {
  console.log("setBackgroundAllowed success");
}).catch((error: BusinessError) => {
  console.log(JSON.stringify(error));
});

policy.isBackgroundAllowed10+

isBackgroundAllowed(callback: AsyncCallback<boolean>): void

Checks whether the current application is allowed to access the network when running at the background. This API uses an asynchronous callback to return the result.

System API: This is a system API.

Required permission: ohos.permission.MANAGE_NET_STRATEGY

System capability: SystemCapability.Communication.NetManager.Core

Parameters

NameTypeMandatoryDescription
callbackAsyncCallback<boolean>YesCallback used to return the result. If the operation is successful, the value true is returned, which means that background applications are allowed to use mobile data. If the operation fails, an error message is returned.

Error codes

IDError Message
201Permission denied.
202Non-system applications use system APIs.
401Parameter error.
2100001Invalid parameter value.
2100002Operation failed. Cannot connect to service.
2100003System internal error.

Example

import { BusinessError } from '@ohos.base';
policy.isBackgroundAllowed((error: BusinessError, data: boolean) => {
  console.log(JSON.stringify(error));
  console.log(JSON.stringify(data));
});

policy.isBackgroundAllowed10+

isBackgroundAllowed(): Promise<boolean>;

Checks whether the current application is allowed to access the network when running at the background. This API uses a promise to return the result.

System API: This is a system API.

Required permission: ohos.permission.MANAGE_NET_STRATEGY

System capability: SystemCapability.Communication.NetManager.Core

Return value

TypeDescription
Promise<boolean>Promise used to return the result. If the operation is successful, the value true is returned, which means that background applications are allowed to use mobile data. If the operation fails, an error message is returned.

Error codes

IDError Message
201Permission denied.
202Non-system applications use system APIs.
401Parameter error.
2100001Invalid parameter value.
2100002Operation failed. Cannot connect to service.
2100003System internal error.

Example

import { BusinessError } from '@ohos.base';
policy
  .isBackgroundAllowed()
  .then((data: boolean) => {
    console.log(JSON.stringify(data));
  })
  .catch((error: BusinessError) => {
    console.log(JSON.stringify(error));
  });

policy.setPolicyByUid10+

setPolicyByUid(uid: number, policy: NetUidPolicy, callback: AsyncCallback<void>): void

Sets the metered network access policy for the application specified by a given UID. This API uses an asynchronous callback to return the result.

System API: This is a system API.

Required permission: ohos.permission.MANAGE_NET_STRATEGY

System capability: SystemCapability.Communication.NetManager.Core

Parameters

NameTypeMandatoryDescription
uidnumberYesUnique ID of the application.
policyNetUidPolicyYesNetwork access policy for the application.
callbackAsyncCallback<void>YesCallback used to return the result. If the operation is successful, no value is returned. If the operation fails, an error message is returned.

Error codes

IDError Message
201Permission denied.
202Non-system applications use system APIs.
401Parameter error.
2100001Invalid parameter value.
2100002Operation failed. Cannot connect to service.
2100003System internal error.

Example

import { BusinessError } from '@ohos.base';
policy.setPolicyByUid(11111, policy.NetUidPolicy.NET_POLICY_NONE, (error: BusinessError) => {
  console.log(JSON.stringify(error));
});

policy.setPolicyByUid10+

setPolicyByUid(uid: number, policy: NetUidPolicy): Promise<void>;

Sets the metered network access policy for the application specified by a given UID. This API uses a promise to return the result.

System API: This is a system API.

Required permission: ohos.permission.MANAGE_NET_STRATEGY

System capability: SystemCapability.Communication.NetManager.Core

Parameters

NameTypeMandatoryDescription
uidnumberYesUnique ID of the application.
policyNetUidPolicyYesNetwork access policy for the application.

Return value

TypeDescription
Promise<void>Promise used to return the result. If the operation is successful, no value is returned. If the operation fails, an error message is returned.

Error codes

IDError Message
201Permission denied.
202Non-system applications use system APIs.
401Parameter error.
2100001Invalid parameter value.
2100002Operation failed. Cannot connect to service.
2100003System internal error.

Example

import { BusinessError } from '@ohos.base';
policy
  .setPolicyByUid(11111, policy.NetUidPolicy.NET_POLICY_NONE)
  .then(() => {
    console.log('setPolicyByUid success');
  })
  .catch((error: BusinessError) => {
    console.log(JSON.stringify(error));
  });

policy.getPolicyByUid10+

getPolicyByUid(uid: number, callback: AsyncCallback<NetUidPolicy>): void

Obtains the network access policy for the application specified by a given UID. This API uses an asynchronous callback to return the result.

System API: This is a system API.

Required permission: ohos.permission.MANAGE_NET_STRATEGY

System capability: SystemCapability.Communication.NetManager.Core

Parameters

NameTypeMandatoryDescription
uidnumberYesUnique ID of the application.
callbackAsyncCallback<NetUidPolicy>YesCallback used to return the result. If the operation is successful, the operation result is returned. If the operation fails, an error message is returned.

Error codes

IDError Message
201Permission denied.
202Non-system applications use system APIs.
401Parameter error.
2100001Invalid parameter value.
2100002Operation failed. Cannot connect to service.
2100003System internal error.

Example

import { BusinessError } from '@ohos.base';
policy.getPolicyByUid(11111, (error: BusinessError, data: object) => {
  console.log(JSON.stringify(error));
  console.log(JSON.stringify(data));
});

policy.getPolicyByUid10+

getPolicyByUid(uid: number): Promise<NetUidPolicy>;

Obtains the network access policy for the application specified by a given UID. This API uses a promise to return the result.

System API: This is a system API.

Required permission: ohos.permission.MANAGE_NET_STRATEGY

System capability: SystemCapability.Communication.NetManager.Core

Parameters

NameTypeMandatoryDescription
uidnumberYesUnique ID of the application.

Return value

TypeDescription
Promise<NetUidPolicy>Promise used to return the result. If the operation fails, an error message is returned.

Error codes

IDError Message
201Permission denied.
202Non-system applications use system APIs.
401Parameter error.
2100001Invalid parameter value.
2100002Operation failed. Cannot connect to service.
2100003System internal error.

Example

import { BusinessError } from '@ohos.base';
policy
  .getPolicyByUid(11111)
  .then((data: policy.NetUidPolicy) => {
    console.log(JSON.stringify(data));
  })
  .catch((error: BusinessError) => {
    console.log(JSON.stringify(error));
  });

policy.getUidsByPolicy10+

getUidsByPolicy(policy: NetUidPolicy, callback: AsyncCallback<Array<number>>): void

Obtains all UIDs that match the specified network policy. This API uses an asynchronous callback to return the result.

System API: This is a system API.

Required permission: ohos.permission.MANAGE_NET_STRATEGY

System capability: SystemCapability.Communication.NetManager.Core

Parameters

NameTypeMandatoryDescription
policyNetUidPolicyYesNetwork policy for the application.
callbackAsyncCallback<Array<number>>YesCallback used to return the result. If the operation is successful, the operation result is returned. If the operation fails, an error message is returned.

Error codes

IDError Message
201Permission denied.
202Non-system applications use system APIs.
401Parameter error.
2100001Invalid parameter value.
2100002Operation failed. Cannot connect to service.
2100003System internal error.

Example

import { BusinessError } from '@ohos.base';
policy.getUidsByPolicy(11111, (error: BusinessError, data: number[]) => {
  console.log(JSON.stringify(error));
  console.log(JSON.stringify(data));
});

policy.getUidsByPolicy10+

getUidsByPolicy(policy: NetUidPolicy): Promise<Array<number>>;

Obtains all UIDs that match the specified network policy. This API uses a promise to return the result.

System API: This is a system API.

Required permission: ohos.permission.MANAGE_NET_STRATEGY

System capability: SystemCapability.Communication.NetManager.Core

Parameters

NameTypeMandatoryDescription
policyNetUidPolicyYesNetwork policy for the application.

Return value

TypeDescription
Promise<Array<number>>Promise used to return the result. If the operation is successful, the operation result is returned. If the operation fails, an error message is returned.

Error codes

IDError Message
201Permission denied.
202Non-system applications use system APIs.
401Parameter error.
2100001Invalid parameter value.
2100002Operation failed. Cannot connect to service.
2100003System internal error.

Example

import { BusinessError } from '@ohos.base';
policy
  .getUidsByPolicy(11111)
  .then((data: object) => {
    console.log(JSON.stringify(data));
  })
  .catch((error: BusinessError) => {
    console.log(JSON.stringify(error));
  });

policy.getNetQuotaPolicies10+

getNetQuotaPolicies(callback: AsyncCallback<Array<NetQuotaPolicy>>): void

Obtains the network quota policies. This API uses an asynchronous callback to return the result.

System API: This is a system API.

Required permission: ohos.permission.MANAGE_NET_STRATEGY

System capability: SystemCapability.Communication.NetManager.Core

Parameters

NameTypeMandatoryDescription
callbackAsyncCallback<Array<NetQuotaPolicy>>YesCallback used to return the result.

Error codes

IDError Message
201Permission denied.
202Non-system applications use system APIs.
401Parameter error.
2100001Invalid parameter value.
2100002Operation failed. Cannot connect to service.
2100003System internal error.

Example

import { BusinessError } from '@ohos.base';
policy.getNetQuotaPolicies((error: BusinessError, data: policy.NetQuotaPolicy[]) => {
  console.log(JSON.stringify(error));
  console.log(JSON.stringify(data));
});

policy.getNetQuotaPolicies10+

getNetQuotaPolicies(): Promise<Array<NetQuotaPolicy>>;

Obtains the network quota policies. This API uses a promise to return the result.

System API: This is a system API.

Required permission: ohos.permission.MANAGE_NET_STRATEGY

System capability: SystemCapability.Communication.NetManager.Core

Return value

TypeDescription
Promise<Array<NetQuotaPolicy>>Promise used to return the result.

Error codes

IDError Message
201Permission denied.
202Non-system applications use system APIs.
401Parameter error.
2100002Operation failed. Cannot connect to service.
2100003System internal error.

Example

import { BusinessError } from '@ohos.base';
policy
  .getNetQuotaPolicies()
  .then((data: policy.NetQuotaPolicy[]) => {
    console.log(JSON.stringify(data));
  })
  .catch((error: BusinessError) => {
    console.log(JSON.stringify(error));
  });

policy.setNetQuotaPolicies10+

setNetQuotaPolicies(quotaPolicies: Array<NetQuotaPolicy>, callback: AsyncCallback<void>): void

Sets network quota policies. This API uses an asynchronous callback to return the result.

System API: This is a system API.

Required permission: ohos.permission.MANAGE_NET_STRATEGY

System capability: SystemCapability.Communication.NetManager.Core

Parameters

NameTypeMandatoryDescription
quotaPoliciesArray<NetQuotaPolicy>YesNetwork quota policies.
callbackAsyncCallback<void>YesCallback used to return the result. If the operation is successful, no value is returned. If the operation fails, an error message is returned.

Error codes

IDError Message
201Permission denied.
202Non-system applications use system APIs.
401Parameter error.
2100001Invalid parameter value.
2100002Operation failed. Cannot connect to service.
2100003System internal error.

Example

import connection from '@ohos.net.connection';
import { BusinessError } from '@ohos.base';

let netQuotaPolicyList: Array<policy.NetQuotaPolicy> = [];
let netquotapolicy: policy.NetQuotaPolicy = {
  networkMatchRule: {
    netType: connection.NetBearType.BEARER_CELLULAR,
    identity: '',
    simId: '1'
  },
  quotaPolicy: {
    periodDuration: 'M1',
    warningBytes: 40000,
    limitBytes: 50000,
    metered: true,
    limitAction: policy.LimitAction.LIMIT_ACTION_NONE
  }
}
netQuotaPolicyList.push(netquotapolicy);

policy.setNetQuotaPolicies(netQuotaPolicyList, (error: BusinessError) => {
  console.log(JSON.stringify(error));
});

policy.setNetQuotaPolicies10+

setNetQuotaPolicies(quotaPolicies: Array<NetQuotaPolicy>): Promise<void>;

Sets network quota policies. This API uses a promise to return the result.

System API: This is a system API.

Required permission: ohos.permission.MANAGE_NET_STRATEGY

System capability: SystemCapability.Communication.NetManager.Core

Parameters

NameTypeMandatoryDescription
quotaPoliciesArray<NetQuotaPolicy>YesNetwork quota policies.

Error codes

IDError Message
201Permission denied.
202Non-system applications use system APIs.
401Parameter error.
2100001Invalid parameter value.
2100002Operation failed. Cannot connect to service.
2100003System internal error.

Return value

TypeDescription
Promise<void>Promise used to return the result. If the operation is successful, no value is returned. If the operation fails, an error message is returned.

Example

import connection from '@ohos.net.connection';
import { BusinessError } from '@ohos.base';

let netQuotaPolicyList: Array<policy.NetQuotaPolicy> = [];
let netquotapolicy: policy.NetQuotaPolicy = {
  networkMatchRule: {
    netType: connection.NetBearType.BEARER_CELLULAR,
    identity: '',
    simId: '1'
  },
  quotaPolicy: {
    periodDuration: 'M1',
    warningBytes: 40000,
    limitBytes: 50000,
    metered: true,
    limitAction: policy.LimitAction.LIMIT_ACTION_NONE
  }
}
netQuotaPolicyList.push(netquotapolicy);

policy
  .setNetQuotaPolicies(netQuotaPolicyList)
  .then(() => {
    console.log('setNetQuotaPolicies success');
  })
  .catch((error: BusinessError) => {
    console.log(JSON.stringify(error));
  });

policy.isUidNetAllowed10+

isUidNetAllowed(uid: number, isMetered: boolean, callback: AsyncCallback<boolean>): void

Checks whether the application specified by a given UID is allowed to access a metered network. This API uses an asynchronous callback to return the result.

System API: This is a system API.

Required permission: ohos.permission.MANAGE_NET_STRATEGY

System capability: SystemCapability.Communication.NetManager.Core

Parameters

NameTypeMandatoryDescription
uidnumberYesUnique ID of the application.
isMeteredbooleanYesWhether the network is a metered network.
callbackAsyncCallback<boolean>YesCallback used to return the result. The value true means that the application is allowed to access metered networks, and the value false means the opposite.

Error codes

IDError Message
201Permission denied.
202Non-system applications use system APIs.
401Parameter error.
2100001Invalid parameter value.
2100002Operation failed. Cannot connect to service.
2100003System internal error.

Example

import { BusinessError } from '@ohos.base';
policy.isUidNetAllowed(11111, true, (error: BusinessError, data: boolean) => {
  console.log(JSON.stringify(error));
  console.log(JSON.stringify(data));
});

policy.isUidNetAllowed10+

isUidNetAllowed(uid: number, isMetered: boolean): Promise<boolean>;

Checks whether the application specified by a given UID is allowed to access a metered network. This API uses a promise to return the result.

System API: This is a system API.

Required permission: ohos.permission.MANAGE_NET_STRATEGY

System capability: SystemCapability.Communication.NetManager.Core

Parameters

NameTypeMandatoryDescription
uidnumberYesUnique ID of the application.
isMeteredbooleanYesWhether the network is a metered network.

Return value

TypeDescription
Promise<boolean>Promise used to return the result.

Error codes

IDError Message
201Permission denied.
202Non-system applications use system APIs.
401Parameter error.
2100001Invalid parameter value.
2100002Operation failed. Cannot connect to service.
2100003System internal error.

Example

import { BusinessError } from '@ohos.base';
policy
  .isUidNetAllowed(11111, true)
  .then((data: boolean) => {
    console.log(JSON.stringify(data));
  })
  .catch((error: BusinessError) => {
    console.log(JSON.stringify(error));
  });

policy.isUidNetAllowed10+

isUidNetAllowed(uid: number, iface: string, callback: AsyncCallback<boolean>): void

Checks whether an application is allowed to access the specified network. This API uses an asynchronous callback to return the result.

System API: This is a system API.

Required permission: ohos.permission.MANAGE_NET_STRATEGY

System capability: SystemCapability.Communication.NetManager.Core

Parameters

NameTypeMandatoryDescription
uidnumberYesUnique ID of the application.
ifacestringYesName of the target network.
callbackAsyncCallback<boolean>YesCallback used to return the result. The value true means that the application is allowed to access the specified network, and the value false means the opposite.

Error codes

IDError Message
201Permission denied.
202Non-system applications use system APIs.
401Parameter error.
2100001Invalid parameter value.
2100002Operation failed. Cannot connect to service.
2100003System internal error.

Example

import { BusinessError } from '@ohos.base';
policy.isUidNetAllowed(11111, 'wlan0', (error: BusinessError, data: boolean) => {
  console.log(JSON.stringify(error));
  console.log(JSON.stringify(data));
});

policy.isUidNetAllowed10+

isUidNetAllowed(uid: number, iface: string): Promise<boolean>;

Checks whether an application is allowed to access the specified network. This API uses a promise to return the result.

System API: This is a system API.

Required permission: ohos.permission.MANAGE_NET_STRATEGY

System capability: SystemCapability.Communication.NetManager.Core

Parameters

NameTypeMandatoryDescription
uidnumberYesUnique ID of the application.
ifacestringYesName of the target network.

Return value

TypeDescription
Promise<boolean>Promise used to return the result.

Error codes

IDError Message
201Permission denied.
202Non-system applications use system APIs.
401Parameter error.
2100001Invalid parameter value.
2100002Operation failed. Cannot connect to service.
2100003System internal error.

Example

import { BusinessError } from '@ohos.base';
policy
  .isUidNetAllowed(11111, 'wlan0')
  .then((data: boolean) => {
    console.log(JSON.stringify(data));
  })
  .catch((error: BusinessError) => {
    console.log(JSON.stringify(error));
  });

policy.setDeviceIdleTrustlist10+

setDeviceIdleTrustlist(uids: Array<number>, isAllowed: boolean, callback: AsyncCallback<void>): void

Adds applications specified by given UIDs to the device idle allowlist. This API uses an asynchronous callback to return the result.

System API: This is a system API.

Required permission: ohos.permission.MANAGE_NET_STRATEGY

System capability: SystemCapability.Communication.NetManager.Core

Parameters

NameTypeMandatoryDescription
uidsArray<number>YesUnique ID of the application.
isAllowedbooleanYesWhether to add the application to the allowlist.
callbackcallback: AsyncCallback<void>YesCallback used to return the result. If the operation is successful, no value is returned. If the operation fails, an error message is returned.

Error codes

IDError Message
201Permission denied.
202Non-system applications use system APIs.
401Parameter error.
2100001Invalid parameter value.
2100002Operation failed. Cannot connect to service.
2100003System internal error.

Example

import { BusinessError } from '@ohos.base';
policy.setDeviceIdleTrustlist([11111, 22222], true, (error: BusinessError) => {
  console.log(JSON.stringify(error));
});

policy.setDeviceIdleTrustlist10+

setDeviceIdleTrustlist(uids: Array<number>, isAllowed: boolean): Promise<void>;

Adds applications specified by given UIDs to the device idle allowlist. This API uses a promise to return the result.

System API: This is a system API.

Required permission: ohos.permission.MANAGE_NET_STRATEGY

System capability: SystemCapability.Communication.NetManager.Core

Parameters

NameTypeMandatoryDescription
uidsArray<number>YesUnique ID of the application.
isAllowedbooleanYesWhether to add the application to the allowlist.

Return value

TypeDescription
Promise<void>Promise used to return the result. If the operation is successful, no value is returned. If the operation fails, an error message is returned.

Error codes

IDError Message
201Permission denied.
202Non-system applications use system APIs.
401Parameter error.
2100001Invalid parameter value.
2100002Operation failed. Cannot connect to service.
2100003System internal error.

Example

import { BusinessError } from '@ohos.base';
policy
  .setDeviceIdleTrustlist([11111, 22222], true)
  .then(() => {
    console.log('setDeviceIdleTrustlist success');
  })
  .catch((error: BusinessError) => {
    console.log(JSON.stringify(error));
  });

policy.getDeviceIdleTrustlist10+

getDeviceIdleTrustlist(callback: AsyncCallback<Array<number>>): void

Obtains the UIDs of applications that are on the device idle allowlist. This API uses an asynchronous callback to return the result.

System API: This is a system API.

Required permission: ohos.permission.MANAGE_NET_STRATEGY

System capability: SystemCapability.Communication.NetManager.Core

Parameters

NameTypeMandatoryDescription
callbackAsyncCallback<Array<number>>YesCallback used to return the result.

Error codes

IDError Message
201Permission denied.
202Non-system applications use system APIs.
401Parameter error.
2100001Invalid parameter value.
2100002Operation failed. Cannot connect to service.
2100003System internal error.

Example

import { BusinessError } from '@ohos.base';
policy.getDeviceIdleTrustlist((error: BusinessError, data: number[]) => {
  console.log(JSON.stringify(error));
  console.log(JSON.stringify(data));
});

policy.getDeviceIdleTrustlist10+

getDeviceIdleTrustlist(): Promise<Array<number>>;

Obtains the UIDs of applications that are on the device idle allowlist. This API uses a promise to return the result.

System API: This is a system API.

Required permission: ohos.permission.MANAGE_NET_STRATEGY

System capability: SystemCapability.Communication.NetManager.Core

Return value

TypeDescription
Promise<Array<number>>Promise used to return the result.

Error codes

IDError Message
201Permission denied.
202Non-system applications use system APIs.
401Parameter error.
2100002Operation failed. Cannot connect to service.
2100003System internal error.

Example

import { BusinessError } from '@ohos.base';
policy
  .getDeviceIdleTrustlist()
  .then((data: number[]) => {
    console.log(JSON.stringify(data));
  })
  .catch((error: BusinessError) => {
    console.log(JSON.stringify(error));
  });

policy.getBackgroundPolicyByUid10+

getBackgroundPolicyByUid(uid: number, callback: AsyncCallback<NetBackgroundPolicy>): void

Obtains the background network policy for the application specified by a given UID. This API uses an asynchronous callback to return the result.

System API: This is a system API.

Required permission: ohos.permission.MANAGE_NET_STRATEGY

System capability: SystemCapability.Communication.NetManager.Core

Parameters

NameTypeMandatoryDescription
uidnumberYesUnique ID of the application.
callbackAsyncCallback<NetBackgroundPolicy>YesCallback used to return the result.

Error codes

IDError Message
201Permission denied.
202Non-system applications use system APIs.
401Parameter error.
2100001Invalid parameter value.
2100002Operation failed. Cannot connect to service.
2100003System internal error.

Example

import { BusinessError } from '@ohos.base';
policy.getBackgroundPolicyByUid(11111, (error: BusinessError, data: number) => {
  console.log(JSON.stringify(error));
  console.log(JSON.stringify(data));
});

policy.getBackgroundPolicyByUid10+

getBackgroundPolicyByUid(uid: number): Promise<NetBackgroundPolicy>;

Obtains the background network policies configured for the specified application. This API uses a promise to return the result.

System API: This is a system API.

Required permission: ohos.permission.MANAGE_NET_STRATEGY

System capability: SystemCapability.Communication.NetManager.Core

Parameters

NameTypeMandatoryDescription
uidnumberYesUnique ID of the application.

Return value

TypeDescription
Promise<NetBackgroundPolicy>Promise used to return the result.

Error codes

IDError Message
201Permission denied.
202Non-system applications use system APIs.
401Parameter error.
2100001Invalid parameter value.
2100002Operation failed. Cannot connect to service.
2100003System internal error.

Example

import { BusinessError } from '@ohos.base';
policy
  .getBackgroundPolicyByUid(11111)
  .then((data: number) => {
    console.log(JSON.stringify(data));
  })
  .catch((error: BusinessError) => {
    console.log(JSON.stringify(error));
  });

policy.resetPolicies10+

resetPolicies(simId: string, callback: AsyncCallback<void>): void

Restores all the policies (cellular network, background network, firewall, and application-specific network policies) for the specified SIM card. This API uses an asynchronous callback to return the result.

System API: This is a system API.

Required permission: ohos.permission.MANAGE_NET_STRATEGY

System capability: SystemCapability.Communication.NetManager.Core

Parameters

NameTypeMandatoryDescription
simIdstringYesSIM card ID.
callbackAsyncCallback<void>YesCallback used to return the result. If the operation is successful, no value is returned. If the operation fails, an error message is returned.

Error codes

IDError Message
201Permission denied.
202Non-system applications use system APIs.
401Parameter error.
2100001Invalid parameter value.
2100002Operation failed. Cannot connect to service.
2100003System internal error.

Example

import { BusinessError } from '@ohos.base';
policy.resetPolicies('1', (error: BusinessError) => {
  console.log(JSON.stringify(error));
});

policy.resetPolicies10+

resetPolicies(simId: string): Promise<void>;

Restores all the policies (cellular network, background network, firewall, and application-specific network policies) for the specified SIM card. This API uses a promise to return the result.

System API: This is a system API.

Required permission: ohos.permission.MANAGE_NET_STRATEGY

System capability: SystemCapability.Communication.NetManager.Core

Parameters

NameTypeMandatoryDescription
simIdstringYesSIM card ID.

Return value

TypeDescription
Promise<void>Promise used to return the result. If the operation is successful, no value is returned. If the operation fails, an error message is returned.

Error codes

IDError Message
201Permission denied.
202Non-system applications use system APIs.
401Parameter error.
2100001Invalid parameter value.
2100002Operation failed. Cannot connect to service.
2100003System internal error.

Example

import { BusinessError } from '@ohos.base';
policy
  .resetPolicies('1')
  .then(() => {
    console.log('resetPolicies success');
  })
  .catch((error: BusinessError) => {
    console.log(JSON.stringify(error));
  });

policy.updateRemindPolicy10+

updateRemindPolicy(netType: NetBearType, simId: string, remindType: RemindType, callback: AsyncCallback<void>): void

Updates a reminder policy. This API uses an asynchronous callback to return the result.

System API: This is a system API.

Required permission: ohos.permission.MANAGE_NET_STRATEGY

System capability: SystemCapability.Communication.NetManager.Core

Parameters

NameTypeMandatoryDescription
netTypeNetBearTypeYesNetwork type.
simIdstringYesSIM card ID.
remindTypeRemindTypeYesReminder type.
callbackAsyncCallback<void>YesCallback used to return the result. If the operation is successful, no value is returned. If the operation fails, an error message is returned.

Error codes

IDError Message
201Permission denied.
202Non-system applications use system APIs.
401Parameter error.
2100001Invalid parameter value.
2100002Operation failed. Cannot connect to service.
2100003System internal error.

Example

import connection from '@ohos.net.connection';
import { BusinessError } from '@ohos.base';
policy.updateRemindPolicy(
  connection.NetBearType.BEARER_CELLULAR,
  '1',
  policy.RemindType.REMIND_TYPE_WARNING,
  (error: BusinessError) => {
    console.log(JSON.stringify(error));
  }
);

policy.updateRemindPolicy10+

updateRemindPolicy(netType: NetBearType, simId: string, remindType: RemindType): Promise<void>;

Updates a reminder policy. This API uses a promise to return the result.

System API: This is a system API.

Required permission: ohos.permission.MANAGE_NET_STRATEGY

System capability: SystemCapability.Communication.NetManager.Core

Parameters

NameTypeMandatoryDescription
netTypeNetBearTypeYesNetwork type.
simIdstringYesSIM card ID.
remindTypeRemindTypeYesReminder type.

Return value

TypeDescription
Promise<void>Promise used to return the result. If the operation is successful, no value is returned. If the operation fails, an error message is returned.

Error codes

IDError Message
201Permission denied.
202Non-system applications use system APIs.
401Parameter error.
2100001Invalid parameter value.
2100002Operation failed. Cannot connect to service.
2100003System internal error.

Example

import connection from '@ohos.net.connection';
import { BusinessError } from '@ohos.base';
policy
  .updateRemindPolicy(
    connection.NetBearType.BEARER_CELLULAR,
    '1',
    policy.RemindType.REMIND_TYPE_WARNING
  )
  .then(() => {
    console.log('updateRemindPolicy success');
  })
  .catch((error: BusinessError) => {
    console.log(JSON.stringify(error));
  });

policy.setPowerSaveTrustlist10+

setPowerSaveTrustlist(uids: Array<number>, isAllowed: boolean, callback: AsyncCallback<void>): void

Sets whether to add the application specified by a given UID to the power-saving allowlist. This API uses an asynchronous callback to return the result.

System API: This is a system API.

Required permission: ohos.permission.MANAGE_NET_STRATEGY

System capability: SystemCapability.Communication.NetManager.Core

Parameters

NameTypeMandatoryDescription
uidsArray<number>YesUnique ID of the application.
isAllowedbooleanYesWhether to add the application to the allowlist.
callbackcallback: AsyncCallback<void>YesCallback used to return the result. If the operation is successful, no value is returned. If the operation fails, an error message is returned.

Error codes

IDError Message
201Permission denied.
202Non-system applications use system APIs.
401Parameter error.
2100001Invalid parameter value.
2100002Operation failed. Cannot connect to service.
2100003System internal error.

Example

import { BusinessError } from '@ohos.base';
policy.setPowerSaveTrustlist([11111, 22222], true, (error: BusinessError) => {
  console.log(JSON.stringify(error));
});

policy.setPowerSaveTrustlist10+

setPowerSaveTrustlist(uids: Array<number>, isAllowed: boolean): Promise<void>;

Sets whether to add the application specified by a given UID to the power-saving allowlist. This API uses a promise to return the result.

System API: This is a system API.

Required permission: ohos.permission.MANAGE_NET_STRATEGY

System capability: SystemCapability.Communication.NetManager.Core

Parameters

NameTypeMandatoryDescription
uidsArray<number>YesUnique ID of the application.
isAllowedbooleanYesWhether to add the application to the allowlist.

Return value

TypeDescription
Promise<void>Promise used to return the result. If the operation is successful, no value is returned. If the operation fails, an error message is returned.

Error codes

IDError Message
201Permission denied.
202Non-system applications use system APIs.
401Parameter error.
2100001Invalid parameter value.
2100002Operation failed. Cannot connect to service.
2100003System internal error.

Example

import { BusinessError } from '@ohos.base';
policy
  .setPowerSaveTrustlist([11111, 22222], true)
  .then(() => {
    console.log('setPowerSaveTrustlist success');
  })
  .catch((error: BusinessError) => {
    console.log(JSON.stringify(error));
  });

policy.getPowerSaveTrustlist10+

getPowerSaveTrustlist(callback: AsyncCallback<Array<number>>): void

Obtains the UID array of applications that are on the power-saving allowlist. This API uses an asynchronous callback to return the result.

System API: This is a system API.

Required permission: ohos.permission.MANAGE_NET_STRATEGY

System capability: SystemCapability.Communication.NetManager.Core

Parameters

NameTypeMandatoryDescription
callbackAsyncCallback<Array<number>>YesCallback used to return the result.

Error codes

IDError Message
201Permission denied.
202Non-system applications use system APIs.
401Parameter error.
2100001Invalid parameter value.
2100002Operation failed. Cannot connect to service.
2100003System internal error.

Example

import { BusinessError } from '@ohos.base';
policy.getPowerSaveTrustlist((error: BusinessError, data: number[]) => {
  console.log(JSON.stringify(error));
  console.log(JSON.stringify(data));
});

policy.getPowerSaveTrustlist10+

getPowerSaveTrustlist(): Promise<Array<number>>;

Obtains the UID array of applications that are on the device idle allowlist. This API uses a promise to return the result.

System API: This is a system API.

Required permission: ohos.permission.MANAGE_NET_STRATEGY

System capability: SystemCapability.Communication.NetManager.Core

Return value

TypeDescription
Promise<Array<number>>Promise used to return the result.

Error codes

IDError Message
201Permission denied.
202Non-system applications use system APIs.
401Parameter error.
2100002Operation failed. Cannot connect to service.
2100003System internal error.

Example

import { BusinessError } from '@ohos.base';
policy
  .getPowerSaveTrustlist()
  .then((data: number[]) => {
    console.log(JSON.stringify(data));
  })
  .catch((error: BusinessError) => {
    console.log(JSON.stringify(error));
  });

policy.on

Represents the handle to a network policy.

on('netUidPolicyChange')10+

on(type: "netUidPolicyChange", callback: Callback<{ uid: number, policy: NetUidPolicy }>): void

Subscribes to policy changes. This API uses an asynchronous callback to return the result.

System API: This is a system API.

Required permission: ohos.permission.MANAGE_NET_STRATEGY

System capability: SystemCapability.Communication.NetManager.Core

Parameters

NameTypeMandatoryDescription
typestringYesEvent type. The value netUidPolicyChange indicates a policy change event.
callbackCallback<{ uid: number, policy: NetUidPolicy }>YesCallback used to return the result. It is called when the registered network policy changes.

Error codes

IDError Message
201Permission denied.
202Non-system applications use system APIs.
401Parameter error.
2100001Invalid parameter value.
2100002Operation failed. Cannot connect to service.
2100003System internal error.

Example

import policy from '@ohos.net.policy';
interface Data {
  uid: number,
  policy: policy.NetUidPolicy
}
policy.on('netUidPolicyChange', (data: Data) => {
  console.log('on netUidPolicyChange: ' + JSON.stringify(data));
});

off('netUidPolicyChange')10+

off(type: "netUidPolicyChange", callback?: Callback<{ uid: number, policy: NetUidPolicy }>): void

Unsubscribes from policy changes. This API uses an asynchronous callback to return the result.

System API: This is a system API.

Required permission: ohos.permission.MANAGE_NET_STRATEGY

System capability: SystemCapability.Communication.NetManager.Core

Parameters

NameTypeMandatoryDescription
typestringYesEvent type. The value netUidPolicyChange indicates a policy change event.
callbackCallback<{ uid: number, policy: NetUidPolicy }>NoCallback used to return the result. It is called when the registered network policy changes.

Error codes

IDError Message
201Permission denied.
202Non-system applications use system APIs.
401Parameter error.
2100001Invalid parameter value.
2100002Operation failed. Cannot connect to service.
2100003System internal error.

Example

let callback = (data: object) => {
  console.log('on netUidPolicyChange, data:' + JSON.stringify(data));
};
policy.on('netUidPolicyChange', callback);
policy.off('netUidPolicyChange', callback);

on('netUidRuleChange')10+

on(type: "netUidRuleChange", callback: Callback<{ uid: number, rule: NetUidRule }>): void

Subscribes to rule changes. This API uses an asynchronous callback to return the result.

System API: This is a system API.

Required permission: ohos.permission.MANAGE_NET_STRATEGY

System capability: SystemCapability.Communication.NetManager.Core

Parameters

NameTypeMandatoryDescription
typestringYesEvent type. The value netUidRuleChange indicates a rule change event.
callbackCallback<{ uid: number, rule: NetUidRule }>YesCallback used to return the result. It is called when the registered rule changes.

Error codes

IDError Message
201Permission denied.
202Non-system applications use system APIs.
401Parameter error.
2100001Invalid parameter value.
2100002Operation failed. Cannot connect to service.
2100003System internal error.

Example

policy.on('netUidRuleChange', (data: object) => {
  console.log('on netUidRuleChange: ' + JSON.stringify(data));
});

off('netUidRuleChange')10+

off(type: "netUidRuleChange", callback?: Callback<{ uid: number, rule: NetUidRule }>): void

Unsubscribes from rule changes. This API uses an asynchronous callback to return the result.

System API: This is a system API.

Required permission: ohos.permission.MANAGE_NET_STRATEGY

System capability: SystemCapability.Communication.NetManager.Core

Parameters

NameTypeMandatoryDescription
typestringYesEvent type. The value netUidRuleChange indicates a rule change event.
callbackCallback<{ uid: number, rule: NetUidRule }>NoCallback used to return the result. It is called when the registered rule changes.

Error codes

IDError Message
201Permission denied.
202Non-system applications use system APIs.
401Parameter error.
2100001Invalid parameter value.
2100002Operation failed. Cannot connect to service.
2100003System internal error.

Example

let callback = (data: object) => {
  console.log('on netUidRuleChange, data:' + JSON.stringify(data));
};
policy.on('netUidRuleChange', callback);
policy.off('netUidRuleChange', callback);

on('netMeteredIfacesChange')10+

on(type: "netMeteredIfacesChange", callback: Callback<Array<string>>): void

Subscribes to metered iface changes. This API uses an asynchronous callback to return the result.

System API: This is a system API.

Required permission: ohos.permission.MANAGE_NET_STRATEGY

System capability: SystemCapability.Communication.NetManager.Core

Parameters

NameTypeMandatoryDescription
typestringYesEvent type. The value netMeteredIfacesChange indicates a metered iface change event.
callbackCallback<Array<string>>YesCallback used to return the result. It is called when the registered metered iface changes.

Error codes

IDError Message
201Permission denied.
202Non-system applications use system APIs.
401Parameter error.
2100001Invalid parameter value.
2100002Operation failed. Cannot connect to service.
2100003System internal error.

Example

policy.on('netMeteredIfacesChange', (data: string[]) => {
  console.log('on netMeteredIfacesChange: ' + JSON.stringify(data));
});

off('netMeteredIfacesChange')10+

off(type: "netMeteredIfacesChange", callback?: Callback<Array<string>>): void

Unsubscribes from metered iface changes. This API uses an asynchronous callback to return the result.

System API: This is a system API.

Required permission: ohos.permission.MANAGE_NET_STRATEGY

System capability: SystemCapability.Communication.NetManager.Core

Parameters

NameTypeMandatoryDescription
typestringYesEvent type. The value netMeteredIfacesChange indicates a metered iface change event.
callbackCallback<Array<string>>NoCallback used to return the result. It is called when the registered metered iface changes.

Error codes

IDError Message
201Permission denied.
202Non-system applications use system APIs.
401Parameter error.
2100001Invalid parameter value.
2100002Operation failed. Cannot connect to service.
2100003System internal error.

Example

let callback = (data: string[]) => {
  console.log('on netMeteredIfacesChange, data:' + JSON.stringify(data));
};
policy.on('netMeteredIfacesChange', callback);
policy.off('netMeteredIfacesChange', callback);

on('netQuotaPolicyChange')10+

on(type: "netQuotaPolicyChange", callback: Callback<Array<NetQuotaPolicy>>): void

Subscribes to network quota policy changes. This API uses an asynchronous callback to return the result.

System API: This is a system API.

Required permission: ohos.permission.MANAGE_NET_STRATEGY

System capability: SystemCapability.Communication.NetManager.Core

Parameters

NameTypeMandatoryDescription
typestringYesEvent type. The value netQuotaPolicyChange indicates a network quota policy change event.
callbackCallback<Array<NetQuotaPolicy>>YesCallback used to return the result. It is called when the registered network quota policy changes.

Error codes

IDError Message
201Permission denied.
202Non-system applications use system APIs.
401Parameter error.
2100001Invalid parameter value.
2100002Operation failed. Cannot connect to service.
2100003System internal error.

Example

policy.on('netQuotaPolicyChange', (data: policy.NetQuotaPolicy[]) => {
  console.log('on netQuotaPolicyChange: ' + JSON.stringify(data));
});

off('netQuotaPolicyChange')10+

off(type: "netQuotaPolicyChange", callback?: Callback<Array<NetQuotaPolicy>>): void

Unsubscribes from network quota policy changes. This API uses an asynchronous callback to return the result.

System API: This is a system API.

Required permission: ohos.permission.MANAGE_NET_STRATEGY

System capability: SystemCapability.Communication.NetManager.Core

Parameters

NameTypeMandatoryDescription
typestringYesEvent type. The value netQuotaPolicyChange indicates a network quota policy change event.
callbackCallback<Array<NetQuotaPolicy>>NoCallback used to return the result. It is called when the registered network quota policy changes.

Error codes

IDError Message
201Permission denied.
202Non-system applications use system APIs.
401Parameter error.
2100001Invalid parameter value.
2100002Operation failed. Cannot connect to service.
2100003System internal error.

Example

policy.on('netQuotaPolicyChange', (data: Array<policy.NetQuotaPolicy>) => {
  console.log('on netQuotaPolicyChange, data:' + JSON.stringify(data));
});
policy.off('netQuotaPolicyChange', (data: Array<policy.NetQuotaPolicy>) => {
  console.log('on netQuotaPolicyChange, data:' + JSON.stringify(data));
});

on('netBackgroundPolicyChange')10+

on(type: "netBackgroundPolicyChange", callback: Callback<boolean>): void

Subscribes to background network policy changes. This API uses an asynchronous callback to return the result.

System API: This is a system API.

Required permission: ohos.permission.MANAGE_NET_STRATEGY

System capability: SystemCapability.Communication.NetManager.Core

Parameters

NameTypeMandatoryDescription
typestringYesEvent type. The value netBackgroundPolicyChange indicates a background network policy change event.
callbackCallback<boolean>YesCallback used to return the result. It is called when the registered background network policy changes.

Error codes

IDError Message
201Permission denied.
202Non-system applications use system APIs.
401Parameter error.
2100001Invalid parameter value.
2100002Operation failed. Cannot connect to service.
2100003System internal error.

Example

policy.on('netBackgroundPolicyChange', (data: boolean) => {
  console.log('on netBackgroundPolicyChange: ' + JSON.stringify(data));
});

off('netBackgroundPolicyChange')10+

off(type: "netBackgroundPolicyChange", callback?: Callback<boolean>): void

Unsubscribes from background network policy changes. This API uses an asynchronous callback to return the result.

System API: This is a system API.

Required permission: ohos.permission.MANAGE_NET_STRATEGY

System capability: SystemCapability.Communication.NetManager.Core

Parameters

NameTypeMandatoryDescription
typestringYesEvent type. The value netBackgroundPolicyChange indicates a background network policy change event.
callbackCallback<boolean>NoCallback used to return the result. It is called when the registered background network policy changes.

Error codes

IDError Message
201Permission denied.
202Non-system applications use system APIs.
401Parameter error.
2100001Invalid parameter value.
2100002Operation failed. Cannot connect to service.
2100003System internal error.

Example

let callback = (data: boolean) => {
  console.log('on netBackgroundPolicyChange, data:' + JSON.stringify(data));
};
policy.on('netBackgroundPolicyChange', callback);
policy.off('netBackgroundPolicyChange', callback);

NetBackgroundPolicy10+

Enumerates the background network policies.

System API: This is a system API.

System capability: SystemCapability.Communication.NetManager.Core

NameValueDescription
NET_BACKGROUND_POLICY_NONE0No background network policy is specified. This is the default value.
NET_BACKGROUND_POLICY_ENABLE1Background applications are allowed to access a metered network.
NET_BACKGROUND_POLICY_DISABLE2Applications running in the background are not allowed to access a metered network.
NET_BACKGROUND_POLICY_TRUSTLIST3Only applications on the allowlist are allowed to access metered networks when they are running in the background.

NetQuotaPolicy10+

Defines the quota policy for the specified network.

System API: This is a system API.

System capability: SystemCapability.Communication.NetManager.Core

NameTypeMandatoryDescription
networkMatchRuleNetworkMatchRuleYesNetwork for which the quota policy is set.
quotaPolicyQuotaPolicyYesNetwork quota policy.

NetworkMatchRule10+

Defines the network for which the quota policy is set.

System API: This is a system API.

System capability: SystemCapability.Communication.NetManager.Core

NameTypeMandatoryDescription
netTypeNetBearTypeYesNetwork type.
simIdstringYesIdentifier of the SIM card on the metered cellular network. It is not used for Wi-Fi networks.
identitystringYesID of the SIM card on the metered cellular network. It is used for Wi-Fi networks. It is used together with iccid.

QuotaPolicy10+

Defines the network quota policy.

System API: This is a system API.

System capability: SystemCapability.Communication.NetManager.Core

NameTypeMandatoryDescription
periodDurationstringYesMetering period for the quota limit. D1, M1, and Y1 indicate one day, one month, and one year, respectively. If the specified metering period is exceeded, the quota is not limited.
warningBytesnumberYesData volume threshold for generating an alarm.
limitBytesnumberYesData volume quota.
meteredstringYesWhether the network is a metered network.
limitActionLimitActionYesAction to take when the data volume quota is reached.
lastWarningRemindstringNoLast time when an alarm was generated.
lastLimitRemindstringNoLast time when the quota was exhausted.

LimitAction10+

Enumerates the actions that can be taken when the data volume quota is reached.

System API: This is a system API.

System capability: SystemCapability.Communication.NetManager.Core

NameValueDescription
LIMIT_ACTION_NONE-1No action is taken. This is the default value.
LIMIT_ACTION_ACCESS_DISABLED0Internet access is disabled.
LIMIT_ACTION_ALERT_ONLY1An alarm is generated when the quota limit is reached.

NetUidRule10+

Enumerates the metered network rules.

System API: This is a system API.

System capability: SystemCapability.Communication.NetManager.Core

NameValueDescription
NET_RULE_NONE0Default rule.
NET_RULE_ALLOW_METERED_FOREGROUND1 << 0Applications running in the foreground are allowed to access a metered network.
NET_RULE_ALLOW_METERED1 << 1Applications are allowed to access a metered network.
NET_RULE_REJECT_METERED1 << 2Applications are not allowed to access a metered network.
NET_RULE_ALLOW_ALL1 << 5Applications are allowed to access all networks (metered or non-metered).
NET_RULE_REJECT_ALL1 << 6Applications are not allowed to access any networks (metered or non-metered).

RemindType10+

Enumerates the reminder types.

System API: This is a system API.

System capability: SystemCapability.Communication.NetManager.Core

NameValueDescription
REMIND_TYPE_WARNING1Warning.
REMIND_TYPE_LIMIT2Limit.

NetUidPolicy10+

Enumerates network access policies for the application.

System API: This is a system API.

System capability: SystemCapability.Communication.NetManager.Core

NameValueDescription
NET_POLICY_NONE0Default network policy.
NET_POLICY_ALLOW_METERED_BACKGROUND1 << 0Background applications are allowed to access a metered network.
NET_POLICY_REJECT_METERED_BACKGROUND1 << 1Applications running in the background are not allowed to access a metered network.

你可能感兴趣的鸿蒙文章

harmony 鸿蒙APIs

harmony 鸿蒙System Common Events (To Be Deprecated Soon)

harmony 鸿蒙System Common Events

harmony 鸿蒙API Reference Document Description

harmony 鸿蒙Enterprise Device Management Overview (for System Applications Only)

harmony 鸿蒙BundleStatusCallback

harmony 鸿蒙@ohos.bundle.innerBundleManager (innerBundleManager)

harmony 鸿蒙@ohos.distributedBundle (Distributed Bundle Management)

harmony 鸿蒙@ohos.bundle (Bundle)

harmony 鸿蒙@ohos.enterprise.EnterpriseAdminExtensionAbility (EnterpriseAdminExtensionAbility)

  • 所属分类: 后端技术
  • 本文标签: 鸿蒙 软件
  • 版权声明: 本文链接 https://seaxiang.com/blog/540b5fad8dc24725a94c3e57d33e6408