openharmony 鸿蒙 js-apis-configPolicy-sys

2025-06-12 浏览 (1)

@ohos.configPolicy (配置策略)(系统接口)

配置策略提供按预先定义的定制配置层级获取对应定制配置目录和文件路径的能力。

说明:

本模块首批接口从API version 8开始支持。后续版本的新增接口,采用上角标单独标记接口的起始版本。

本模块接口均为系统接口。

导入模块

import configPolicy from '@ohos.configPolicy';

getOneCfgFile

getOneCfgFile(relPath: string, callback: AsyncCallback<string>)

使用callback形式返回指定文件名的最高优先级配置文件路径。 例如,config.xml在设备中存在以下路径(优先级从低到高):/system/etc/config.xml、/sys_pod/etc/config.xml,最终返回/sys_pod/etc/config.xml。

系统能力:SystemCapability.Customization.ConfigPolicy

参数:

参数名类型必填说明
relPathstring配置文件名。
callbackAsyncCallback<string>异步回调,用于返回最高优先级配置文件的路径。

错误码

以下错误码的详细介绍请参见通用错误码

错误码ID错误信息
401Parameter error. Possible causes: 1.Mandatory parameters are left unspecified; 2.Incorrect parameter types.

示例:

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

try {
  let relpath: string = 'etc/config.xml';
  configPolicy.getOneCfgFile(relpath, (error: BusinessError, value: string) => {
    if (error == null) {
      console.log('value is ' + value);
    } else {
      console.error('error: ' + error.code + ', ' + error.message);
    }
  });
} catch (error) {
  let code = (error as BusinessError).code;
  let message = (error as BusinessError).message;
  console.error('error:' + code + ', ' + message);
}

getOneCfgFile

getOneCfgFile(relPath: string): Promise<string>

使用Promise形式返回指定文件名的最高优先级配置文件路径。

系统能力:SystemCapability.Customization.ConfigPolicy

参数:

参数名类型必填说明
relPathstring配置文件名。

错误码

以下错误码的详细介绍请参见通用错误码

错误码ID错误信息
401Parameter error. Possible causes: 1.Mandatory parameters are left unspecified; 2.Incorrect parameter types.

返回值:

类型说明
Promise<string>返回最高优先级配置文件的路径。

示例:

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

try {
  let relpath: string = 'etc/config.xml';
  configPolicy.getOneCfgFile(relpath).then((value: string) => {
    console.log('value is ' + value);
  }).catch((error: BusinessError) => {
    console.error('getOneCfgFile promise error: ' + error.code + ', ' + error.message);
  });
} catch (error) {
  let code = (error as BusinessError).code;
  let message = (error as BusinessError).message;
  console.error('error:' + code + ', ' + message);
}

getCfgFiles

getCfgFiles(relPath: string, callback: AsyncCallback<Array<string>>)

按优先级从低到高,使用callback形式返回指定文件名所有的文件列表。 例如,config.xml在设备中存在以下路径(优先级从低到高):/system/etc/config.xml、/sys_pod/etc/config.xml,最终返回/system/etc/config.xml, /sys_pod/etc/config.xml。

系统能力:SystemCapability.Customization.ConfigPolicy

参数:

参数名类型必填说明
relPathstring配置文件名。
callbackAsyncCallback<Array<string>>异步回调,用于返回文件列表。

错误码

以下错误码的详细介绍请参见通用错误码

错误码ID错误信息
401Parameter error. Possible causes: 1.Mandatory parameters are left unspecified; 2.Incorrect parameter types.

示例:

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

try {
  configPolicy.getCfgFiles('etc/config.xml', (error: BusinessError, value: Array<string>) => {
    if (error == null) {
      console.log('value is ' + value);
    } else {
      console.error('error: ' + error.code + ', ' + error.message);
    }
  });
} catch (error) {
  let code = (error as BusinessError).code;
  let message = (error as BusinessError).message;
  console.error('error:' + code + ', ' + message);
}

getCfgFiles

getCfgFiles(relPath: string): Promise<Array<string>>

按优先级从低到高,使用Promise形式返回指定文件名所有的文件列表。

系统能力:SystemCapability.Customization.ConfigPolicy

参数:

参数名类型必填说明
relPathstring配置文件名。

错误码

以下错误码的详细介绍请参见通用错误码

错误码ID错误信息
401Parameter error. Possible causes: 1.Mandatory parameters are left unspecified; 2.Incorrect parameter types.

返回值:

类型说明
Promise<Array<string>>返回文件列表。

示例:

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

try {
  let relpath: string = 'etc/config.xml';
  configPolicy.getCfgFiles(relpath).then((value: Array<string>) => {
    console.log('value is ' + value);
  }).catch((error: BusinessError) => {
    console.error('getCfgFiles promise error: ' + error.code + ', ' + error.message);
  });
} catch (error) {
  let code = (error as BusinessError).code;
  let message = (error as BusinessError).message;
  console.error('error:' + code + ', ' + message);
}

getCfgDirList

getCfgDirList(callback: AsyncCallback<Array<string>>)

使用callback形式返回配置层级目录列表。

系统能力:SystemCapability.Customization.ConfigPolicy

参数:

参数名类型必填说明
callbackAsyncCallback<Array<string>>异步回调,用于返回配置层级目录列表。

错误码

以下错误码的详细介绍请参见通用错误码

错误码ID错误信息
401Parameter error. Possible causes: 1.Mandatory parameters are left unspecified; 2.Incorrect parameter types.

示例:

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

try {
  configPolicy.getCfgDirList((error: BusinessError, value: Array<string>) => {
    if (error == null) {
      console.log('value is ' + value);
    } else {
      console.error('error: ' + error.code + ', ' + error.message);
    }
  });
} catch (error) {
  let code = (error as BusinessError).code;
  let message = (error as BusinessError).message;
  console.error('error:' + code + ', ' + message);
}

getCfgDirList

getCfgDirList(): Promise<Array<string>>

使用Promise形式返回配置层级目录列表。

系统能力:SystemCapability.Customization.ConfigPolicy

返回值:

类型说明
Promise<Array<string>>返回配置层级目录列表。

示例:

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

try {
  configPolicy.getCfgDirList().then((value: Array<string>) => {
    console.log('value is ' + value);
  }).catch((error: BusinessError) => {
    console.error('getCfgDirList promise error: ' + error.code + ', ' + error.message);
  });
} catch (error) {
  let code = (error as BusinessError).code;
  let message = (error as BusinessError).message;
  console.error('error:' + code + ', ' + message);
}

getOneCfgFile11+

getOneCfgFile(relPath: string, followMode: FollowXMode, callback: AsyncCallback<string>)

根据提供的跟随模式获取指定文件名的最高优先级配置文件路径,并使用callback形式返回。 例如,config.xml在设备中存在以下路径(优先级从低到高):/system/etc/config.xml、/sys_pod/etc/config.xml、/sys_pod/etc/carrier/46060/etc/config.xml,且设备默认卡opkey为46060,设置的followMode为configPolicy.FollowXMode.SIM_DEFAULT,最终返回/sys_pod/etc/carrier/46060/etc/config.xml。

系统能力:SystemCapability.Customization.ConfigPolicy

参数:

参数名类型必填说明
relPathstring配置文件名。
followModeFollowXMode跟随模式。
callbackAsyncCallback<string>异步回调,用于返回最高优先级配置文件的路径。

错误码

以下错误码的详细介绍请参见通用错误码

错误码ID错误信息
401Parameter error. Possible causes: 1.Mandatory parameters are left unspecified; 2.Incorrect parameter types.

示例:

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

try {
  let relpath: string = 'etc/config.xml';
  configPolicy.getOneCfgFile(relpath, configPolicy.FollowXMode.SIM_DEFAULT,
    (error: BusinessError, value: string) => {
    if (error == null) {
      console.log('value is ' + value);
    } else {
      console.error('error: ' + error.code + ', ' + error.message);
    }
  });
} catch (error) {
  let code = (error as BusinessError).code;
  let message = (error as BusinessError).message;
  console.error('error:' + code + ', ' + message);
}

getOneCfgFile11+

getOneCfgFile(relPath: string, followMode: FollowXMode, extra: string, callback: AsyncCallback<string>)

根据提供的跟随模式获取指定文件名的最高优先级配置文件路径,并使用callback形式返回。 例如,config.xml在设备中存在以下路径(优先级从低到高):/system/etc/config.xml、/sys_pod/etc/config.xml、/sys_pod/etc/carrier/46060/etc/config.xml,且设备卡1的opkey为46060,设置的followMode为configPolicy.FollowXMode.USER_DEFINED,自定义跟随规则为"etc/carrier/${telephony.sim.opkey0}",最终返回/sys_pod/etc/carrier/46060/etc/config.xml。

系统能力:SystemCapability.Customization.ConfigPolicy

参数:

参数名类型必填说明
relPathstring配置文件名。
followModeFollowXMode跟随模式。
extrastring用户自定义跟随规则,仅在followMode为USER_DEFINED时有效。
callbackAsyncCallback<string>异步回调,用于返回最高优先级配置文件的路径。

错误码

以下错误码的详细介绍请参见通用错误码

错误码ID错误信息
401Parameter error. Possible causes: 1.Mandatory parameters are left unspecified; 2.Incorrect parameter types.

示例:

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

try {
  let relpath: string = 'etc/config.xml';
  let extra: string = 'etc/carrier/${telephony.sim.opkey0}';
  configPolicy.getOneCfgFile(relpath, configPolicy.FollowXMode.USER_DEFINED, extra,
    (error: BusinessError, value: string) => {
    if (error == null) {
      console.log('value is ' + value);
    } else {
      console.error('error: ' + error.code + ', ' + error.message);
    }
  });
} catch (error) {
  let code = (error as BusinessError).code;
  let message = (error as BusinessError).message;
  console.error('error:' + code + ', ' + message);
}

getOneCfgFile11+

getOneCfgFile(relPath: string, followMode: FollowXMode, extra?: string): Promise<string>

根据提供的跟随模式获取指定文件名的最高优先级配置文件路径,并使用Promise形式返回。

系统能力:SystemCapability.Customization.ConfigPolicy

参数:

参数名类型必填说明
relPathstring配置文件名。
followModeFollowXMode跟随模式。
extrastring用户自定义跟随规则,仅在followMode为USER_DEFINED时必填。

错误码

以下错误码的详细介绍请参见通用错误码

错误码ID错误信息
401Parameter error. Possible causes: 1.Mandatory parameters are left unspecified; 2.Incorrect parameter types; 3.Parameter verification failed.

返回值:

类型说明
Promise<string>返回最高优先级配置文件的路径。

示例:

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

try {
  let relpath: string = 'etc/config.xml';
  let extra: string = 'etc/carrier/${telephony.sim.opkey0}';
  configPolicy.getOneCfgFile(relpath, configPolicy.FollowXMode.SIM_DEFAULT, extra).then((value: string) => {
    console.log('value is ' + value);
  }).catch((error: BusinessError) => {
    console.error('getOneCfgFile promise error: ' + error.code + ', ' + error.message);
  });
} catch (error) {
  let code = (error as BusinessError).code;
  let message = (error as BusinessError).message;
  console.error('error:' + code + ', ' + message);
}

getOneCfgFileSync11+

getOneCfgFileSync(relPath: string, followMode?: FollowXMode, extra?: string): string

根据提供的跟随模式返回指定文件名的最高优先级配置文件路径。

系统能力:SystemCapability.Customization.ConfigPolicy

参数:

参数名类型必填说明
relPathstring配置文件名。
followModeFollowXMode跟随模式,不设置时,默认使用DEFAULT。
extrastring用户自定义跟随规则,仅在followMode为USER_DEFINED时必填。

错误码

以下错误码的详细介绍请参见通用错误码

错误码ID错误信息
401Parameter error. Possible causes: 1.Mandatory parameters are left unspecified; 2.Incorrect parameter types; 3.Parameter verification failed.

返回值:

类型说明
string返回最高优先级配置文件的路径。

示例:

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

try {
  let relpath: string = 'etc/config.xml';
  let extra: string = 'etc/carrier/${telephony.sim.opkey0}';
  let result: string = configPolicy.getOneCfgFileSync(relpath, configPolicy.FollowXMode.USER_DEFINED, extra);
  console.log('result is ' + result);
} catch (error) {
  let code = (error as BusinessError).code;
  let message = (error as BusinessError).message;
  console.error('error:' + code + ', ' + message);
}

getCfgFiles11+

getCfgFiles(relPath: string, followMode: FollowXMode, callback: AsyncCallback<Array<string>>)

按优先级从低到高,根据提供的跟随模式获取指定文件名所有的文件列表,并使用callback形式返回。 例如,config.xml在设备中存在以下路径(优先级从低到高):/system/etc/config.xml、/sys_pod/etc/config.xml、/sys_pod/etc/carrier/46060/etc/config.xml,且设备默认卡opkey为46060,设置的followMode为configPolicy.FollowXMode.SIM_DEFAULT,最终返回/system/etc/config.xml, /sys_pod/etc/config.xml, /sys_pod/etc/carrier/46060/etc/config.xml。

系统能力:SystemCapability.Customization.ConfigPolicy

参数:

参数名类型必填说明
relPathstring配置文件名。
followModeFollowXMode跟随模式。
callbackAsyncCallback<Array<string>>异步回调,用于返回文件列表。

错误码

以下错误码的详细介绍请参见通用错误码

错误码ID错误信息
401Parameter error. Possible causes: 1.Mandatory parameters are left unspecified; 2.Incorrect parameter types.

示例:

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

try {
  let relpath: string = 'etc/config.xml';
  configPolicy.getCfgFiles(relpath, configPolicy.FollowXMode.SIM_DEFAULT,
    (error: BusinessError, value: Array<string>) => {
    if (error == null) {
      console.log('value is ' + value);
    } else {
      console.error('error: ' + error.code + ', ' + error.message);
    }
  });
} catch (error) {
  let code = (error as BusinessError).code;
  let message = (error as BusinessError).message;
  console.error('error:' + code + ', ' + message);
}

getCfgFiles11+

getCfgFiles(relPath: string, followMode: FollowXMode, extra: string, callback: AsyncCallback<Array<string>>)

按优先级从低到高,根据提供的跟随模式获取指定文件名所有的文件列表,并使用callback形式返回。 例如,config.xml在设备中存在以下路径(优先级从低到高):/system/etc/config.xml、/sys_pod/etc/config.xml、/sys_pod/etc/carrier/46060/etc/config.xml,且设备卡1的opkey为46060,设置的followMode为configPolicy.FollowXMode.USER_DEFINED,自定义跟随规则为"etc/carrier/${telephony.sim.opkey0}",最终返回/system/etc/config.xml, /sys_pod/etc/config.xml, /sys_pod/etc/carrier/46060/etc/config.xml。

系统能力:SystemCapability.Customization.ConfigPolicy

参数:

参数名类型必填说明
relPathstring配置文件名。
followModeFollowXMode跟随模式。
extrastring用户自定义跟随规则,仅在followMode为USER_DEFINED时有效。
callbackAsyncCallback<Array<string>>异步回调,用于返回文件列表。

错误码

以下错误码的详细介绍请参见通用错误码

错误码ID错误信息
401Parameter error. Possible causes: 1.Mandatory parameters are left unspecified; 2.Incorrect parameter types.

示例:

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

try {
  let relpath: string = 'etc/config.xml';
  let extra: string = 'etc/carrier/${telephony.sim.opkey0}';
  configPolicy.getCfgFiles(relpath, configPolicy.FollowXMode.SIM_DEFAULT, extra,
    (error: BusinessError, value: Array<string>) => {
    if (error == null) {
      console.log('value is ' + value);
    } else {
      console.error('error: ' + error.code + ', ' + error.message);
    }
  });
} catch (error) {
  let code = (error as BusinessError).code;
  let message = (error as BusinessError).message;
  console.error('error:' + code + ', ' + message);
}

getCfgFiles11+

getCfgFiles(relPath: string, followMode: FollowXMode, extra?: string): Promise<Array<string>>

根据提供的跟随模式按优先级从低到高,获取指定文件名所有的文件列表,并使用Promise形式返回。

系统能力:SystemCapability.Customization.ConfigPolicy

参数:

参数名类型必填说明
relPathstring配置文件名。
followModeFollowXMode跟随模式。
extrastring用户自定义跟随规则,仅在followMode为USER_DEFINED时必填。

错误码

以下错误码的详细介绍请参见通用错误码

错误码ID错误信息
401Parameter error. Possible causes: 1.Mandatory parameters are left unspecified; 2.Incorrect parameter types; 3.Parameter verification failed.

返回值:

类型说明
Promise<Array<string>>返回文件列表。

示例:

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

try {
  let relpath: string = 'etc/config.xml';
  let extra: string = 'etc/carrier/${telephony.sim.opkey0}';
  configPolicy.getCfgFiles(relpath, configPolicy.FollowXMode.SIM_DEFAULT, extra).then((value: Array<string>) => {
    console.log('value is ' + value);
  }).catch((error: BusinessError) => {
    console.error('getCfgFiles promise error: ' + error.code + ', ' + error.message);
  });
} catch (error) {
  let code = (error as BusinessError).code;
  let message = (error as BusinessError).message;
  console.error('error:' + code + ', ' + message);
}

getCfgFilesSync11+

getCfgFilesSync(relPath: string, followMode?: FollowXMode, extra?: string): Array<string>

根据提供的跟随模式返回指定文件名所有的文件列表。

系统能力:SystemCapability.Customization.ConfigPolicy

参数:

参数名类型必填说明
relPathstring配置文件名。
followModeFollowXMode跟随模式,不设置时,默认使用DEFAULT。
extrastring用户自定义跟随规则,仅在followMode为USER_DEFINED时必填。

错误码

以下错误码的详细介绍请参见通用错误码

错误码ID错误信息
401Parameter error. Possible causes: 1.Mandatory parameters are left unspecified; 2.Incorrect parameter types; 3.Parameter verification failed.

返回值:

类型说明
Array<string>返回文件列表。

示例:

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

try {
  let relpath: string = 'etc/config.xml';
  let extra: string = 'etc/carrier/${telephony.sim.opkey0}';
  let result: Array<string> = configPolicy.getCfgFilesSync(relpath, configPolicy.FollowXMode.USER_DEFINED, extra);
  console.log('result is ' + result);
} catch (error) {
  let code = (error as BusinessError).code;
  let message = (error as BusinessError).message;
  console.error('error:' + code + ', ' + message);
}

getCfgDirListSync11+

getCfgDirListSync(): Array<string>

返回配置层级目录列表。

系统能力:SystemCapability.Customization.ConfigPolicy

返回值:

类型说明
Array<string>返回配置层级目录列表。

示例:

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

try {
  let result: Array<string> = configPolicy.getCfgDirListSync();
  console.log('result is ' + result);
} catch (error) {
  let code = (error as BusinessError).code;
  let message = (error as BusinessError).message;
  console.error('error:' + code + ', ' + message);
}

FollowXMode11+

系统能力: SystemCapability.Customization.ConfigPolicy

名称说明
DEFAULT0默认模式,此模式下会根据各配置层级下的followx_file_list.cfg文件配置的跟随规则进行文件查找。
NO_RULE_FOLLOWED1不跟随模式,此模式下不会使用任何跟随规则,即使存在followx_file_list.cfg文件。
SIM_DEFAULT10跟随默认卡模式,此模式下会根据默认卡的opkey在各配置层级下的etc/carrier/${opkey}下查找文件。
SIM_111跟随卡1模式,此模式下会根据卡1的opkey在各配置层级下的etc/carrier/${opkey}下查找文件。
SIM_212跟随卡2模式,此模式下会根据卡2的opkey在各配置层级下的etc/carrier/${opkey}下查找文件。
USER_DEFINED100用户自定义模式,此模式会根据入参extra提供的跟随规则进行配置文件获取。此模式会忽略各配置层级下的followx_file_list.cfg文件。

你可能感兴趣的鸿蒙文章

harmony 鸿蒙Basic Services Kit(基础服务)

harmony 鸿蒙DeviceInfo

harmony 鸿蒙OH_Print

harmony 鸿蒙Print_Margin

harmony 鸿蒙Print_PageSize

harmony 鸿蒙Print_PrintAttributes

harmony 鸿蒙Print_PrintDocCallback

harmony 鸿蒙Print_Range

harmony 鸿蒙TimeService

harmony 鸿蒙ohscan.h

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