动态订阅公共事件


动态订阅公共事件

场景介绍

动态订阅是指当应用在运行状态时对某个公共事件进行订阅,在运行期间如果有订阅的事件发布那么订阅了这个事件的应用将会收到该事件及其传递的参数。例如,某应用希望在其运行期间收到电量过低的事件,并根据该事件降低其运行功耗,那么该应用便可动态订阅电量过低事件,收到该事件后关闭一些非必要的任务来降低功耗。订阅部分系统公共事件需要先申请权限,订阅这些事件所需要的权限请见公共事件权限列表

接口说明

详细接口见接口文档

接口名 接口描述
createSubscriber(subscribeInfo: CommonEventSubscribeInfo, callback: AsyncCallback<CommonEventData>): void 创建订阅者对象(callback)
createSubscriber(subscribeInfo: CommonEventSubscribeInfo): Promise<CommonEventSubscriber> 创建订阅者对象(promise)
subscribe(subscriber: CommonEventSubscriber, callback: AsyncCallback): void 订阅公共事件

开发步骤

  1. 导入模块。

    import commonEventManager from '@ohos.commonEventManager';
    
    1
  2. 创建订阅者信息,详细的订阅者信息数据类型及包含的参数请见CommonEventSubscribeInfo文档介绍。

    // 用于保存创建成功的订阅者对象,后续使用其完成订阅及退订的动作
    let subscriber = null;
    // 订阅者信息
    let subscribeInfo = {
        events: ["usual.event.SCREEN_OFF"], // 订阅灭屏公共事件
    }
    
    1
    2
    3
    4
    5
    6
  3. 创建订阅者,保存返回的订阅者对象subscriber,用于执行后续的订阅、退订等操作。

    // 创建订阅者回调
    commonEventManager.createSubscriber(subscribeInfo, (err, data) => {
        if (err) {
            console.error(`[CommonEvent] CreateSubscriberCallBack err=${JSON.stringify(err)}`);
        } else {
            console.info(`[CommonEvent] CreateSubscriber success`);
            subscriber = data;
            // 订阅公共事件回调
        }
    })
    
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
  4. 创建订阅回调函数,订阅回调函数会在接收到事件时触发。订阅回调函数返回的data内包含了公共事件的名称、发布者携带的数据等信息,公共事件数据的详细参数和数据类型请见CommonEventData文档介绍。

    // 订阅公共事件回调
    if (subscriber !== null) {
        commonEventManager.subscribe(subscriber, (err, data) => {
            if (err) {
                console.error(`[CommonEvent] SubscribeCallBack err=${JSON.stringify(err)}`);
            } else {
                console.info(`[CommonEvent] SubscribeCallBack data=${JSON.stringify(data)}`);
            }
        })
    } else {
        console.error(`[CommonEvent] Need create subscriber`);
    }
    
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12