commonEvent 定义介绍
发布:发送公共事件,事件表示事件名称。PublishAsUser:发送指定用户的公共事件。CreateSubscriber:创建事件的订户。订阅:订阅事件,可以是公共事件或自定义事件。取消订阅:取消订阅事件。一旦取消,将不会接收后续事件。@哦。commonEvent 模块提供了一个简单的 API。首先,创建一个订阅服务器来接收事件,然后开始订阅事件,最后取消订阅。要发布事件,可以直接调用 publish()方法来发布事件。如果事件与订阅者订阅的事件类型匹配,则会将其回调给订阅者。简单步骤如下:
declare namespace commonEvent { function publish(event: string, callback: AsyncCallback<void>): void; function publish(event: string, options: CommonEventPublishData, callback: AsyncCallback<void>): void; function publishAsUser(event: string, userId: number, callback: AsyncCallback<void>): void; function publishAsUser(event: string, userId: number, options: CommonEventPublishData, callback: AsyncCallback<void>): void; function createSubscriber(subscribeInfo: CommonEventSubscribeInfo, callback: AsyncCallback<CommonEventSubscriber>): void; function createSubscriber(subscribeInfo: CommonEventSubscribeInfo): Promise<CommonEventSubscriber>; function subscribe(subscriber: CommonEventSubscriber, callback: AsyncCallback<CommonEventData>): void; function unsubscribe(subscriber: CommonEventSubscriber, callback?: AsyncCallback<void>): void;}
复制代码
限制与约束
@ohos.reminderAgent 模块里提供了发布后台代理提醒和取消后台代理提醒的相关 API,部分 API 如下所示:
declare namespace reminderAgent { // 发送后台代理提醒 function publishReminder(reminderReq: ReminderRequest, callback: AsyncCallback<number>): void; // 取消后台代理提醒 function cancelReminder(reminderId: number, callback: AsyncCallback<void>): void; // 获取所有后台代理提醒 function getValidReminders(callback: AsyncCallback<Array<ReminderRequest>>): void; // 取消所有后台代理提醒 function cancelAllReminders(callback: AsyncCallback<void>): void; // 省略部分API }
复制代码
发布提醒:发布后台代理提醒。ReminderRequest 参数描述如下:ReminderType:设置后台提醒类型,支持以下三种类型:提醒_类型_计时器:倒计时提醒。使用此类型时,需要使用 ReminderRequestTimer 配置 reminderReq 参数。提醒_类型_日历:日历提醒。使用此类型时,需要使用 ReminderRequestCalendar 配置 reminderReq 参数。提醒_类型_警报:闹钟提醒。使用此类型时,reminderReq 参数需要配置为 ReminderRequestAlarm。ActionButton:弹出提醒通知栏中显示的按钮(可选参数,支持 0/1/2 按钮)。WantAgent:单击通知后要跳转到的目标功能信息。maxScreenWantAgent:提醒目标包在到达时跳转。如果设备正在使用,则会弹出一个通知框。RingDuration:表示振铃持续时间。SnoozeTimes:表示延迟提醒的次数。TimeInterval:执行延迟提醒间隔。标题:表示警报标题。内容:表示提醒内容。ExpiredContent:表示提醒过期后要显示的内容。snoozeContent:表示提醒延迟时要显示的内容。NotificationId:表示提醒使用的通知的 ID 号。具有相同 ID 号的提醒将被覆盖。插槽类型:表示提醒的插槽类型。取消提醒:取消后台代理提醒。GetValidReminders:获取当前应用程序设置的所有有效(未过期)提醒。取消所有提醒:取消当前应用程序中的所有提醒。
private cancelAllTimer() { reminderAgent.cancelAllReminders((error, data) => { if(!error) { // 取消成功 } })}
复制代码
思路如下:专用 startTimer(){让计时器={reminderType:reminderAgent.reminderType.REMINDER_TYPE_TIMER,触发器时间(秒):10}提醒代理。publishReminder(计时器,(error,reminderId)=>{如果(reminderId){// 发送成功,如果需要取消该提醒,这要使用该提醒 ID}})}
原子化服务代码简析
创建原子化服务项目后,系统将默认在条目的 js 目录和 FormAbility 目录中生成页面模板和服务管理。页面模板是标准的原子化卡片布局,页面表示页面集,索引表示特定模块。服务管理为服务状态更改提供回调,其中可以更新卡数据。
<div class="container"> <stack> // 堆叠式布局 <div class="container-img"> <image src="/common/widget.png" class="bg-img"/> // 设备卡片背景 </div> <div class="container-inner"> <text class="title"> // 设置标题 {{ $t('strings.title_immersive') }} </text> <text class="detail_text" onclick="routerEvent"> // 设备内容,点击时触发routerEvent事件 {{ $t('strings.detail_immersive') }} </text> </div> </stack></div>
复制代码
class 为 "detail_text" 的 text 组件添加了 onclick 事件,当点击该组件时触发 routerEvent 的事件回调。routerEvent 在 index.json 文件中配置,布局代码演示效果如下:
Canvas
ArkUI 开发框架提供了 Canvas Canvas 组件,以支持我们对自绘各种图形的需求。Canvas 特定绘图委托 CanvasRenderingContext2D 执行。CanvasRenderingContext2D 提供了一系列与绘画相关的方法。作者简单地将 CanvasRenderingContext2D 理解为画笔,画笔绘制的内容显示在 Canvas 画布上。本课程主要介绍 CanvasRenderingContext2D 的使用。
interface CanvasInterface { (context?: CanvasRenderingContext2D): CanvasAttribute;}
复制代码
上下文:创建 Canvas 组件时,需要一个 CanvasRenderingContext2D 实例,该实例负责在 Canvas 上绘制特定内容,如文本、图片和各种形状。
@Entry @Component struct Index {
// 初始化RenderingContextSettings并设置为抗锯齿 private setting: RenderingContextSettings = new RenderingContextSettings(true); // 初始化CanvasRenderingContext2D private context: CanvasRenderingContext2D = new CanvasRenderingContext2D(this.setting);
build() { Column() { Canvas(this.context) // 设置Canvas所需要的context .size({width: '100%', height: "100%"}) // 设置Canvas的宽高 .onReady(() => { // 监听回调,在回调内执行绘画操作 this.context.fillRect(10, 10, 130, 40); // 以(10, 10)为起点坐标,画矩形,默认黑色 }) } .width('100%') .height('100%') }}
复制代码
评论