HarmonyOS API:设备管理

joytrian
发布于 2023-4-10 17:49
浏览
0收藏

版本:v3.1 Beta

@ohos.stationary (设备状态感知框架)

更新时间: 2023-02-17 09:19


设备状态感知框架提供设备状态感知能力,包括绝对静止和相对静止。


说明

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

本模块不支持x86模拟器。

导入模块

import stationary from '@ohos.stationary'

ActivityResponse

服务响应抽象接口。

系统能力 SystemCapability.Msdp.DeviceStatus.Stationary

属性

名称

参数类型

可读

可写

说明

state

​ActivityState​

设备状态变化返回值。

ActivityType

设备状态类型。

系统能力 SystemCapability.Msdp.DeviceStatus.Stationary

名称

描述

still

绝对静止。

relativeStill

相对静止。

ActivityEvent

设备状态事件。

系统能力 SystemCapability.Msdp.DeviceStatus.Stationary

变量

说明

ENTER

1

进入。

EXIT

2

退出。

ENTER_EXIT

3

进入和退出。

ActivityState

设备状态返回值。

系统能力 SystemCapability.Msdp.DeviceStatus.Stationary

变量

说明

ENTER

1

进入。

EXIT

2

退出。

stationary.on('still' | 'relativeStill')

on(activity: ActivityType, event: ActivityEvent, reportLatencyNs: number, callback: Callback<ActivityResponse>): void

设备状态管理,订阅设备状态服务。

系统能力 SystemCapability.Msdp.DeviceStatus.Stationary

参数:

名称

类型

必填

说明

activity

​ActivityType​

设备状态能力类型。

event

​ActivityEvent​

事件类型。

reportLatencyNs

number

报告延时。

callback

Callback<​​ActivityResponse​​>

回调函数,接收上报状态变化事件。

示例:

var reportLatencyNs = 100;
stationary.on('still', stationary.ActivityEvent.ENTER, reportLatencyNs, (data) => {
    console.log('data='+ JSON.stringify(data));
})

stationary.once('still' | 'relativeStill')

once(activity: ActivityType, callback: Callback<ActivityResponse>): void

设备状态管理,查询设备状态。

系统能力 SystemCapability.Msdp.DeviceStatus.Stationary

参数:

名称

类型

必填

说明

activity

​ActivityType​

设备状态能力类型。

callback

Callback<​​ActivityResponse​​>

回调函数,接收上报状态变化事件。

示例:

stationary.once('still', (data) => {
    console.log("data="+ JSON.stringify(data));
})

stationary.off('still' | 'relativeStill')

off(activity: ActivityType, event: ActivityEvent, callback?: Callback<ActivityResponse>): void

设备状态管理,取消订阅设备状态服务。

系统能力 SystemCapability.Msdp.DeviceStatus.Stationary

参数:

名称

类型

必填

说明

activity

​ActivityType​

设备状态能力类型。

event

​ActivityEvent​

事件类型。

callback

Callback<​​ActivityResponse​​>

回调函数,接收上报状态变化事件。

示例:

stationary.off('still', stationary.ActivityEvent.ENTER);

@ohos.thermal (热管理)

更新时间: 2023-02-17 09:19


该模块提供热管理相关的接口,包括热档位查询及注册回调等功能。


说明

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

导入模块

import thermal from '@ohos.thermal';

thermal.registerThermalLevelCallback9+

registerThermalLevelCallback(callback: Callback<ThermalLevel>): void

订阅热档位变化时的回调提醒。

系统能力: SystemCapability.PowerManager.ThermalManager

参数:

参数名

类型

必填

说明

callback

Callback<ThermalLevel>

回调函数,返回变化后的热档位。

错误码:

以下错误码的详细介绍请参见​​耗电统计错误码​​。

错误码ID

错误信息

4800101

连接服务失败。

示例:

try {
    thermal.registerThermalLevelCallback(level => {
        console.info('thermal level is: ' + level);
    });
    console.info('register thermal level callback success.');
} catch(err) {
    console.error('register thermal level callback failed, err: ' + err);
}

thermal.unregisterThermalLevelCallback9+

unregisterThermalLevelCallback(callback?: Callback<void>): void

取消订阅热档位变化时的回调提醒。

系统能力: SystemCapability.PowerManager.ThermalManager

参数:

参数名

类型

必填

说明

callback

Callback<void>

回调函数,无返回值。不填该参数则取消所有回调。

错误码:

以下错误码的详细介绍请参见​​热管理错误码​​。

错误码ID

错误信息

4800101

连接服务失败。

示例:

try {
    thermal.unregisterThermalLevelCallback(() => {
        console.info('unsubscribe thermal level success.');
    });
    console.info('unregister thermal level callback success.');
} catch(err) {
    console.error('unregister thermal level callback failed, err: ' + err);
}

thermal.getLevel9+

getLevel(): ThermalLevel

获取当前热档位信息。

系统能力: SystemCapability.PowerManager.ThermalManager

返回值:

类型

说明

ThermalLevel

热档位信息。

错误码:

以下错误码的详细介绍请参见​​热管理错误码​​。

错误码ID

错误信息

4800101

连接服务失败。

示例:

try {
    var level = thermal.getLevel();
    console.info('thermal level is: ' + level);
} catch(err) {
    console.error('get thermal level failed, err: ' + err);
}

thermal.subscribeThermalLevel(deprecated)

subscribeThermalLevel(callback: AsyncCallback<ThermalLevel>): void

说明

从API version 9开始不再维护,建议使用​​thermal.registerThermalLevelCallback​​替代。

订阅热档位变化时的回调提醒。

系统能力: SystemCapability.PowerManager.ThermalManager

参数:

参数名

类型

必填

说明

callback

AsyncCallback<ThermalLevel>

回调函数。AsyncCallback只返回一个参数,为热档位信息,此时可能会产生告警,可通过// @ts-ignore进行抑制。

示例:

thermal.subscribeThermalLevel((level) => {
    console.info('thermal level is: ' + level);
});

thermal.unsubscribeThermalLevel(deprecated)

unsubscribeThermalLevel(callback?: AsyncCallback<void>): void

说明

从API version 9开始不再维护,建议使用​​thermal.unregisterThermalLevelCallback​​替代。

取消订阅热档位变化时的回调提醒。

系统能力: SystemCapability.PowerManager.ThermalManager

参数:

参数名

类型

必填

说明

callback

AsyncCallback<void>

回调函数,无返回值。不填该参数则取消所有回调。

示例:

thermal.unsubscribeThermalLevel(() => {
    console.info('unsubscribe thermal level success.');
});

thermal.getThermalLevel(deprecated)

getThermalLevel(): ThermalLevel

说明

从API version 9开始不再维护,建议使用​​thermal.getLevel​​替代。

获取当前热档位信息。

系统能力: SystemCapability.PowerManager.ThermalManager

返回值:

类型

说明

ThermalLevel

热档位信息。

示例:

var level = thermal.getThermalLevel();
console.info('thermal level is: ' + level);

ThermalLevel

热档位信息。

系统能力: SystemCapability.PowerManager.ThermalManager

名称

说明

COOL

0

表明设备处于低温的状态,业务执行不受热控的限制。

NORMAL

1

表明设备处于正常工作状态,但温度不低,需要注意是否临近发热状态

WARM

2

表明设备已经进入温热状态,部分无感知业务需要考虑停止或延迟执行。

HOT

3

表明设备已经明显发热,无感知业务应全面停止,其他业务应考虑降规格及负载。

OVERHEATED

4

表明设备已经发热严重,无感知业务应全面停止,主要业务需降低规格及负载。

WARNING

5

表明设备已经发热严重并且即将进入紧急状态,无感知业务应全面停止,主要业务应降低至最低规格。

EMERGENCY

6

表明设备已经进入紧急状态,所有业务应当全面停止工作,可保留部分紧急求助功能。


文章转载自:​​https://developer.harmonyos.com/cn/docs/documentation/doc-references-V3/js-apis-stationary-0000001427745104-V3?catalogVersion=V3​

已于2023-4-10 17:49:56修改
收藏
回复
举报
回复
    相关推荐