可参考如下demo://Index.ets import { BusinessError } from '@ohos.base'; import { calendarMgr } from '../entryability/EntryAbility'; import { calendarManager } from '@kit.CalendarKit'; @Entry @Component struct Index { build() { Column() { Button("添加日程").onClick(()=>{ let calendar : calendarManager.Calendar | undefined = undefined; const date = new Date(); const event: calendarManager.Event = { type: calendarManager.EventType.NORMAL, title:"参加读书会", startTime: date.getTime(), endTime: date.getTime() + 60 * 60 * 1000 }; calendarMgr?.getCalendar().then((data: calendarManager.Calendar) => { console.info(`Succeeded to get calendar, data -> ${JSON.stringify(data)}`); calendar = data; calendar.addEvent(event, (err: BusinessError, data: number): void => { if (err) { console.error(`Failed to addEvent, err -> ${JSON.stringify(err)}`); } else { console.info(`Succeeded to add event, id -> ${data}`); } }); }).catch((err: BusinessError) => { console.error(`Failed to get calendar, err -> ${JSON.stringify(err)}`); }); }) } .height('100%') } }//EntryAbility .ets import AbilityConstant from '@ohos.app.ability.AbilityConstant'; import hilog from '@ohos.hilog'; import UIAbility from '@ohos.app.ability.UIAbility'; import Want from '@ohos.app.ability.Want'; import window from '@ohos.window'; import { ContextUtil } from '../ContextUtil'; import { Configuration } from '@ohos.app.ability.Configuration'; import { abilityAccessCtrl, common, ConfigurationConstant, PermissionRequestResult, Permissions } from '@kit.AbilityKit'; import { BusinessError } from '@kit.BasicServicesKit'; import { calendarManager } from '@kit.CalendarKit'; export let calendarMgr: calendarManager.CalendarManager | null = null; export let mContext: common.UIAbilityContext | null = null; export default class EntryAbility extends UIAbility { onCreate(want: Want, launchParam: AbilityConstant.LaunchParam): void { hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onCreate'); ContextUtil.mContext = this.context; ContextUtil.mConfiguration = this.context.config; } onDestroy(): void { hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onDestroy'); } onWindowStageCreate(windowStage: window.WindowStage): void { // Main window is created, set main page for this ability hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onWindowStageCreate'); windowStage.loadContent('pages/Index25', (err, data) => { if (err.code) { hilog.error(0x0000, 'testTag', 'Failed to load the content. Cause: %{public}s', JSON.stringify(err) ?? ''); return; } hilog.info(0x0000, 'testTag', 'Succeeded in loading the content. Data: %{public}s', JSON.stringify(data) ?? ''); }); mContext = this.context; const permissions: Permissions[] = ['ohos.permission.READ_CALENDAR', 'ohos.permission.WRITE_CALENDAR']; let atManager = abilityAccessCtrl.createAtManager(); atManager.requestPermissionsFromUser(mContext, permissions).then((result: PermissionRequestResult) => { console.log(`get Permission success, result: ${JSON.stringify(result)}`); calendarMgr = calendarManager.getCalendarManager(mContext); }).catch((error: BusinessError) => { console.error(`get Permission error, error: ${JSON.stringify(error)}`); }) } onWindowStageDestroy(): void { // Main window is destroyed, release UI related resources hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onWindowStageDestroy'); } onForeground(): void { // Ability has brought to foreground hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onForeground'); } onBackground(): void { // Ability has back to background hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onBackground'); } onConfigurationUpdate(newConfig: Configuration): void { ContextUtil.mConfiguration = newConfig; } }//module.json5 "requestPermissions": [ { "name": "ohos.permission.READ_CALENDAR", "reason": "$string:reason", "usedScene": { "abilities": [ "EntryAbility" ], "when": "inuse" } }, { "name": "ohos.permission.WRITE_CALENDAR", "reason": "$string:reason", "usedScene": { "abilities": [ "EntryAbility" ], "when": "inuse" } } ]参考文档:https://developer.huawei.com/consumer/cn/doc/harmonyos-references-V5/js-apis-calendarmanager-V5\#event
可参考如下demo:
参考文档:
https://developer.huawei.com/consumer/cn/doc/harmonyos-references-V5/js-apis-calendarmanager-V5\#event