openharmony 鸿蒙 js-apis-system-parameterEnhance-sys

2025-06-12 浏览 (1)

@ohos.systemParameterEnhance (System Parameter) (System API)

The SystemParameter module provides system services with easy access to key-value pairs. You can use the APIs provided by this module to describe the service status and change the service behavior. The basic operation primitives are get and set. You can obtain the values of system parameters through getter APIs and modify the values through setter APIs. For details about the system parameter design principles and definitions, see Parameter Management.

NOTE

  • The initial APIs of this module are supported since API version 9. Newly added APIs will be marked with a superscript to indicate their earliest API version.
  • The APIs provided by this module are system APIs.
  • Third-party applications cannot use the APIs provided by this module, because system parameters each require specific discretionary access control (DAC) and mandatory access control (MAC) permissions.

Modules to Import

import { systemParameterEnhance } from '@kit.BasicServicesKit';

systemParameterEnhance.getSync

getSync(key: string, def?: string): string

Obtains the value of the system parameter with the specified key.

System capability: SystemCapability.Startup.SystemInfo

Parameters

NameTypeMandatoryDescription
keystringYesKey of the system parameter. The value can contain a maximum of 128 bytes. Only letters, digits, periods (.), hyphens (-), at signs (@), colons (:), and underscores (_) are allowed.
defstringNoDefault value of the system parameter.
It works only when the system parameter does not exist.
The value can be undefined or any custom value.

Return value

TypeDescription
stringValue of the system parameter.
If the specified key exists, the set value is returned.
If the specified key does not exist and def is set to a valid value, the set value is returned. If the specified key does not exist and def is set to an invalid value (such as undefined) or is not set, an exception is thrown.

Error codes

IDError Message
401Parameter error. Possible causes: 1.Mandatory parameters are left unspecified; 2.incorrect parameter types; 3.parameter verification failed.
14700101System parameter not found.
14700103The operation on the system permission is denied.
14700104System internal error such as out memory or deadlock.

For details about the error codes, see System Parameter Error Codes.

Example

try {
    let info: string = systemParameterEnhance.getSync("const.ohos.apiversion");
    console.log(JSON.stringify(info));
} catch(e) {
    console.log("getSync unexpected error: " + e);
}

systemParameterEnhance.get

get(key: string, callback: AsyncCallback<string>): void

Obtains the value of the system parameter with the specified key.

System capability: SystemCapability.Startup.SystemInfo

Parameters

NameTypeMandatoryDescription
keystringYesKey of the system parameter. The value can contain a maximum of 128 bytes. Only letters, digits, periods (.), hyphens (-), at signs (@), colons (:), and underscores (_) are allowed.
callbackAsyncCallback<string>YesCallback used to return the result.

Error codes

IDError Message
401Parameter error. Possible causes: 1.Mandatory parameters are left unspecified; 2.incorrect parameter types; 3.parameter verification failed.
14700101System parameter not found.
14700103The operation on the system permission is denied.
14700104System internal error such as out memory or deadlock.

For details about the error codes, see System Parameter Error Codes.

Example

import { BusinessError } from '@kit.BasicServicesKit';

try {
    systemParameterEnhance.get("const.ohos.apiversion", (err: BusinessError, data: string) => {
    if (err == undefined) {
        console.log("get test.parameter.key value success:" + data)
    } else {
        console.log(" get test.parameter.key value err:" + err.code)
    }});
} catch(e) {
    console.log("get unexpected error: " + e);
}

systemParameterEnhance.get

get(key: string, def: string, callback: AsyncCallback<string>): void

Obtains the value of the system parameter with the specified key. This API uses an asynchronous callback to return the result.

System capability: SystemCapability.Startup.SystemInfo

Parameters

NameTypeMandatoryDescription
keystringYesKey of the system parameter. The value can contain a maximum of 128 bytes. Only letters, digits, periods (.), hyphens (-), at signs (@), colons (:), and underscores (_) are allowed.
defstringYesDefault value.
callbackAsyncCallback<string>YesCallback used to return the result.

Error codes

IDError Message
401Parameter error. Possible causes: 1.Mandatory parameters are left unspecified; 2.incorrect parameter types; 3.parameter verification failed.
14700101System parameter not found.
14700103The operation on the system permission is denied.
14700104System internal error such as out memory or deadlock.

For details about the error codes, see System Parameter Error Codes.

Example

import { BusinessError } from '@kit.BasicServicesKit';

try {
    systemParameterEnhance.get("const.ohos.apiversion", "default", (err: BusinessError, data: string) => {
        if (err == undefined) {
            console.log("get test.parameter.key value success:" + data)
        } else {
            console.log(" get test.parameter.key value err:" + err.code)
        }
    });
} catch(e) {
    console.log("get unexpected error:" + e)
}

systemParameterEnhance.get

get(key: string, def?: string): Promise<string>

Obtains the value of the system parameter with the specified key. This API uses a promise to return the result.

System capability: SystemCapability.Startup.SystemInfo

Parameters

NameTypeMandatoryDescription
keystringYesKey of the system parameter. The value can contain a maximum of 128 bytes. Only letters, digits, periods (.), hyphens (-), at signs (@), colons (:), and underscores (_) are allowed.
defstringNoDefault value of the system parameter.
It works only when the system parameter does not exist.
The value can be undefined or any custom value.

Return value

TypeDescription
Promise<string>Promise used to return the execution result.

Error codes

IDError Message
401Parameter error. Possible causes: 1.Mandatory parameters are left unspecified; 2.incorrect parameter types; 3.parameter verification failed.
14700101System parameter not found.
14700103The operation on the system permission is denied.
14700104System internal error such as out memory or deadlock.

For details about the error codes, see System Parameter Error Codes.

Example

import { BusinessError } from '@kit.BasicServicesKit';

try {
    let p: Promise<string> = systemParameterEnhance.get("const.ohos.apiversion");
    p.then((value: string) => {
        console.log("get test.parameter.key success: " + value);
    }).catch((err: BusinessError) => {
        console.log("get test.parameter.key error: " + err.code);
    });
} catch(e) {
    console.log("get unexpected error: " + e);
}

systemParameterEnhance.setSync

setSync(key: string, value: string): void

Sets a value for the system parameter with the specified key.

System capability: SystemCapability.Startup.SystemInfo

Parameters

NameTypeMandatoryDescription
keystringYesKey of the system parameter. The value can contain a maximum of 128 bytes. Only letters, digits, periods (.), hyphens (-), at signs (@), colons (:), and underscores (_) are allowed.
valuestringYesValue of the system parameter to set. The value can contain a maximum of 96 bytes (including the end character).

Error codes

IDError Message
401Parameter error. Possible causes: 1.Mandatory parameters are left unspecified; 2.incorrect parameter types; 3.parameter verification failed.
14700102Invalid system parameter value.
14700103The operation on the system permission is denied.
14700104System internal error such as out memory or deadlock.

For details about the error codes, see System Parameter Error Codes.

Example

import { BusinessError } from '@kit.BasicServicesKit';

try {
    systemParameterEnhance.setSync("test.parameter.key", "default");
} catch(e) {
    console.log("set unexpected error: " + e);
}

systemParameterEnhance.set

set(key: string, value: string, callback: AsyncCallback<void>): void

Sets a value for the system parameter with the specified key. This API uses an asynchronous callback to return the result.

System capability: SystemCapability.Startup.SystemInfo

Parameters

NameTypeMandatoryDescription
keystringYesKey of the system parameter. The value can contain a maximum of 128 bytes. Only letters, digits, periods (.), hyphens (-), at signs (@), colons (:), and underscores (_) are allowed.
valuestringYesValue of the system parameter to set. The value can contain a maximum of 96 bytes (including the end character).
callbackAsyncCallback<void>YesCallback used to return the result.

Error codes

IDError Message
401Parameter error. Possible causes: 1.Mandatory parameters are left unspecified; 2.incorrect parameter types; 3.parameter verification failed.
14700102Invalid system parameter value.
14700103The operation on the system permission is denied.
14700104System internal error such as out memory or deadlock.

For details about the error codes, see System Parameter Error Codes.

Example

import { BusinessError } from '@kit.BasicServicesKit';

try {
    systemParameterEnhance.set("test.parameter.key", "testValue", (err: BusinessError, data: void) => {
    if (err == undefined) {
        console.log("set test.parameter.key value success :" + data)
    } else {
        console.log("set test.parameter.key value err:" + err.code)
    }});
} catch(e) {
    console.log("set unexpected error: " + e);
}

systemParameterEnhance.set

set(key: string, value: string): Promise<void>

Sets a value for the system parameter with the specified key. This API uses a promise to return the result.

System capability: SystemCapability.Startup.SystemInfo

Parameters

NameTypeMandatoryDescription
keystringYesKey of the system parameter. The value can contain a maximum of 128 bytes. Only letters, digits, periods (.), hyphens (-), at signs (@), colons (:), and underscores (_) are allowed.
valuestringYesValue of the system parameter to set. The value can contain a maximum of 96 bytes (including the end character).

Return value

TypeDescription
Promise<void>Promise used to return the execution result.

Error codes

IDError Message
401Parameter error. Possible causes: 1.Mandatory parameters are left unspecified; 2.incorrect parameter types; 3.parameter verification failed.
14700102Invalid system parameter value.
14700103The operation on the system permission is denied.
14700104System internal error such as out memory or deadlock.

For details about the error codes, see System Parameter Error Codes.

Example

import { BusinessError } from '@kit.BasicServicesKit';

try {
    let p: Promise<void>  = systemParameterEnhance.set("test.parameter.key", "testValue");
    p.then((value: void) => {
        console.log("set test.parameter.key success: " + value);
    }).catch((err: BusinessError) => {
        console.log(" set test.parameter.key error: " + err.code);
    });
} catch(e) {
    console.log("set unexpected error: " + e);
}

你可能感兴趣的鸿蒙文章

harmony 鸿蒙Basic Services Kit

harmony 鸿蒙DeviceInfo

harmony 鸿蒙InitSync

harmony 鸿蒙OH_Print

harmony 鸿蒙OsAccount

harmony 鸿蒙Pasteboard

harmony 鸿蒙Print_Margin

harmony 鸿蒙Print_PageSize

harmony 鸿蒙Print_PrintAttributes

harmony 鸿蒙Print_PrintDocCallback

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