harmony 鸿蒙@ohos.systemParameterEnhance (System Parameter)

  • 2023-06-24
  • 浏览 (275)

@ohos.systemParameterEnhance (System Parameter)

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 getters and modify the values through setters.

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 systemparameter from '@ohos.systemParameterEnhance';

systemparameter.getSync

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

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

System capability: SystemCapability.Startup.SystemInfo

Parameters

Name Type Mandatory Description
key string Yes Key of the system parameter.
def string No Default 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

Type Description
string Value 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

ID Error Message
14700101 if key is not found
14700103 if permission denied
14700104 if system internal error

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

Example

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

systemparameter.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

Name Type Mandatory Description
key string Yes Key of the system parameter.
callback AsyncCallback<string> Yes Callback used to return the result.

Error codes

ID Error Message
14700101 if key is not found
14700103 if permission denied
14700104 if system internal error

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

Example

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

try {
    systemparameter.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);
}

systemparameter.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

Name Type Mandatory Description
key string Yes Key of the system parameter.
def string Yes Default value.
callback AsyncCallback<string> Yes Callback used to return the result.

Error codes

ID Error Message
14700101 if key is not found
14700103 if permission denied
14700104 if system internal error

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

Example

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

try {
    systemparameter.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)
}

systemparameter.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

Name Type Mandatory Description
key string Yes Key of the system parameter.
def string No Default 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

Type Description
Promise<string> Promise used to return the execution result.

Error codes

ID Error Message
14700101 if key is not found
14700103 if permission denied
14700104 if system internal error

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

Example

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

try {
    let p: Promise<string> = systemparameter.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);
}

systemparameter.setSync

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

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

System capability: SystemCapability.Startup.SystemInfo

Parameters

Name Type Mandatory Description
key string Yes Key of the system parameter.
value string Yes Value of the system parameter to set.

Error codes

ID Error Message
14700102 if value is invalid
14700103 if permission denied
14700104 if system internal error

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

Example

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

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

systemparameter.set

set(key: string, value: string, callback: AsyncCallback&lt;void&gt;): 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

Name Type Mandatory Description
key string Yes Key of the system parameter.
value string Yes Value of the system parameter to set.
callback AsyncCallback&lt;void&gt; Yes Callback used to return the result.

Error codes

ID Error Message
14700102 if value is invalid
14700103 if permission denied
14700104 if system internal error

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

Example

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

try {
    systemparameter.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);
}

systemparameter.set

set(key: string, value: string): Promise&lt;void&gt;

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

Name Type Mandatory Description
key string Yes Key of the system parameter.
value string Yes Value of the system parameter to set.

Return value

Type Description
Promise&lt;void&gt; Promise used to return the execution result.

Error codes

ID Error Message
14700102 if value is invalid
14700103 if permission denied
14700104 if system internal error

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

Example

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

try {
    let p: Promise<void>  = systemparameter.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 鸿蒙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)

0  赞