harmony 鸿蒙@ohos.systemParameter (System Parameter)

  • 2022-08-09
  • 浏览 (792)

@ohos.systemParameter (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 APIs of this module are no longer maintained since API version 9. It is recommended that you use @ohos.systemParameterEnhance instead. - The initial APIs of this module are supported since API version 6. 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 MAC permissions.

Modules to Import

import systemparameter from '@ohos.systemparameter';

systemparameter.getSync(deprecated)

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 empty string is returned.

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(deprecated)

get(key: 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.
callback AsyncCallback<string> Yes Callback used to return the result.

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(deprecated)

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.

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(deprecated)

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.

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(deprecated)

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.

NOTE - This API can be used only for setting parameters of system applications. - SELinux and Discretionary Access Control (DAC) rules must be configured for authorized system applications. For details about how to configure SELinux and DAC rules, see Parameter Management.

Example

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

systemparameter.set(deprecated)

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.

NOTE - This API can be used only for setting parameters of system applications. - SELinux and Discretionary Access Control (DAC) rules must be configured for authorized system applications. For details about how to configure SELinux and DAC rules, see Parameter Management.

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(deprecated)

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.

NOTE - This API can be used only for setting parameters of system applications. - SELinux and Discretionary Access Control (DAC) rules must be configured for authorized system applications. For details about how to configure SELinux and DAC rules, see Parameter Management.

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  赞