公共事件订阅


公共事件订阅

场景介绍

当需要订阅某个公共事件,获取该公共事件传递的参数时,需要创建一个订阅者对象,用于作为订阅公共事件的载体,订阅公共事件并获取公共事件传递而来的参数。订阅部分系统公共事件需要先申请权限,订阅这些事件所需要的权限请见公共事件权限列表

接口说明

详细接口见接口文档

接口名 接口描述
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