@ohos.promptAction(弹窗):是一种创建并显示文本提示框、对话框和操作菜单的API,在API10以后可以通过使用UIContext中的getPromptAction方法获取当前UI上下文关联的PromptAction对象。该方式在不设置页面级弹窗属性的前提下,弹窗总是显示在所有page页面的最上面。
由于默认情况下弹窗的显示层级高于所有的page页面,所以只需要在弹窗内部实现一个计时器,并在进入应用时执行,满足条件时自动弹出即可。
以Toast弹窗为例:
import { window } from '@kit.ArkUI';
export class GlobalTimer {
static timeID: number;
static checkTime() {
let now = new Date();
let hour = now.getHours();
let minutes = now.getMinutes();
// 如果当前时间是9点,且分钟是23分
if (hour === 9 && minutes === 23) {
// 这里放置想要定时执行的代码
let mainWindow: ESObject = (AppStorage.get('windowStage') as window.WindowStage).getMainWindowSync();
mainWindow.getUIContext().getPromptAction().showToast({
message: '定时任务触发!!!!', // 显示内容。
duration: 10000 // 提醒弹窗显示10秒钟。
});
}
}
// 每分钟检查一次时间
public static toast() {
GlobalTimer.timeID = setInterval(GlobalTimer.checkTime, 60000);
}
clearTime() {
clearInterval(GlobalTimer.timeID);
}
} import {UIAbility} from '@kit.AbilityKit';
import { hilog } from '@kit.PerformanceAnalysisKit';
import { window } from '@kit.ArkUI';
import { GlobalTimer } from '../pages/GlobalTimer';
const DOMAIN = 0x0000;
export default class EntryAbility extends UIAbility {
onDestroy(): void {
hilog.info(DOMAIN, 'testTag', '%{public}s', 'Ability onDestroy');
}
onWindowStageCreate(windowStage: window.WindowStage): void {
// Main window is created, set main page for this ability
hilog.info(DOMAIN, 'testTag', '%{public}s', 'Ability onWindowStageCreate');
windowStage.loadContent('pages/Index', (err) => {
AppStorage.setOrCreate('windowStage', windowStage);
GlobalTimer.toast();
if (err.code) {
hilog.error(DOMAIN, 'testTag', 'Failed to load the content. Cause: %{public}s', JSON.stringify(err));
return;
}
hilog.info(DOMAIN, 'testTag', 'Succeeded in loading the content.');
});
}
onWindowStageDestroy(): void {
// Main window is destroyed, release UI related resources
hilog.info(DOMAIN, 'testTag', '%{public}s', 'Ability onWindowStageDestroy');
}
onForeground(): void {
// Ability has brought to foreground
hilog.info(DOMAIN, 'testTag', '%{public}s', 'Ability onForeground');
}
onBackground(): void {
// Ability has back to background
hilog.info(DOMAIN, 'testTag', '%{public}s', 'Ability onBackground');
}
}; 该处封装的Toast弹窗,其它更多样式布局弹窗详见不依赖UI组件的全局自定义弹出框(openCustomDialog)。
@Entry
@Component
struct Index59 {
message: string = '主页';
build() {
RelativeContainer() {
Text(this.message)
.id('Index59HelloWorld')
.fontSize(35)
.fontWeight(FontWeight.Bold)
.alignRules({
center: { anchor: '__container__', align: VerticalAlign.Center },
middle: { anchor: '__container__', align: HorizontalAlign.Center }
})
}
.height('100%')
.width('100%');
}
}