# 系统属性 - [导入模块](#zh-cn_topic_0000001094819718_section370mcpsimp) - [权限列表](#zh-cn_topic_0000001094819718_section373mcpsimp) - [方法](#zh-cn_topic_0000001094819718_section1319529172015) - [getSync\(key: string, def?: string\)](#zh-cn_topic_0000001094819718_section3381192816421) - [get\(key: string, callback: AsyncCallback\)](#zh-cn_topic_0000001094819718_section19655131534912) - [get\(key: string, def: string, callback: AsyncCallback\)](#zh-cn_topic_0000001094819718_section045334733915) - [get\(key: string, def?: string\)](#zh-cn_topic_0000001094819718_section10288162818402) - [setSync\(key: string, value: string\)](#zh-cn_topic_0000001094819718_section63102185493) - [set\(key: string, value: string, callback: AsyncCallback\)](#zh-cn_topic_0000001094819718_section18770184911197) - [set\(key: string, def?: string\)](#zh-cn_topic_0000001094819718_section187724496193) ## 导入模块 ``` import parameter from '@ohos.systemParameter' ``` ## 权限列表 无 ## 方法 ## getSync\(key: string, def?: string\) 获取系统属性Key对应的值。 **参数:** 参数名 类型 必填 说明 key string 是 待查询的系统属性Key。 def string 否 默认值。 **返回值:** 类型 说明 string 系统属性值,若key不存在,返回默认值。若未指定默认值,返回空字符串。 **示例:** ``` try { var info = parameter.getSync("test.parameter.key"); console.log(JSON.stringify(info)); }catch(e){ console.log("getSync unexpected error: " + e); } ``` ## get\(key: string, callback: AsyncCallback\) 获取系统属性Key对应的值。 **参数:** 参数名 类型 必填 说明 key string 是 待查询的系统属性Key。 callback AsyncCallback<string> 是 回调函数。 **返回值:** 无 **示例:** ``` try { parameter.get("test.parameter.key", function (err, data) { 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); } ``` ## get\(key: string, def: string, callback: AsyncCallback\) 获取系统属性Key对应的值。 **参数:** 参数名 类型 必填 说明 key string 是 待查询的系统属性Key。 def string 是 默认值。 callback AsyncCallback<string> 是 回调函数。 **返回值:** 无 **示例:** ``` try { parameter.get("test.parameter.key", "default", function (err, data) { 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) } ``` ## get\(key: string, def?: string\) 获取系统属性Key对应的值。 **参数:** 参数名 类型 必填 说明 key string 是 待查询的系统属性Key。 def string 否 默认值。 **返回值:** 类型 说明 Promise<string> Promise示例,用于异步获取结果。 **示例:** ``` try { var p = parameter.get("test.parameter.key"); p.then(function (value) { console.log("get test.parameter.key success: " + value); }).catch(function (err) { console.log("get test.parameter.key error: " + err.code); }); }catch(e){ console.log("get unexpected error: " + e); } ``` ## setSync\(key: string, value: string\) 设置系统属性Key对应的值。 **参数:** 参数名 类型 必填 说明 key string 是 待设置的系统属性Key。 value string 是 待设置的系统属性值。 **返回值:** 无 **示例:** ``` try { parameter.setSync("test.parameter.key", "default"); }catch(e){ console.log("set unexpected error: " + e); } ``` ## set\(key: string, value: string, callback: AsyncCallback\) 设置系统属性Key对应的值。 **参数:** 参数名 类型 必填 说明 key string 是 待查询的系统属性Key。 def string 是 默认值。 callback AsyncCallback<void> 是 回调函数。 **返回值:** 无 **示例:** ``` try { parameter.set("test.parameter.key", "testValue", function (err, data) { 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); } ``` ## set\(key: string, def?: string\) 设置系统属性Key对应的值。 **参数:** 参数名 类型 必填 说明 key string 是 待查询的系统属性Key。 def string 否 默认值。 **返回值:** 类型 说明 Promise<string> Promise示例,用于异步获取结果。 **示例:** ``` try { var p = para.set("test.parameter.key", "testValue"); p.then(function (value) { console.log("set test.parameter.key success: " + value); }).catch(function (err) { console.log(" set test.parameter.key error: " + err.code); }); }catch(e){ console.log("set unexpected error: " + e); } ```
参数名
类型
必填
说明
key
string
是
待查询的系统属性Key。
def
否
默认值。
系统属性值,若key不存在,返回默认值。若未指定默认值,返回空字符串。
callback
AsyncCallback<string>
回调函数。
Promise<string>
Promise示例,用于异步获取结果。
待设置的系统属性Key。
value
待设置的系统属性值。
AsyncCallback<void>