##HarmonyOS Next实战##HarmonyOS SDK应用服务##教育##
目标:实现APP启动时,启动页为广告,并且5秒后可手动关闭广告,跳转至主页。
思路:
- 入口页面应设置为启动页
- 启动页通过定时器,在倒计时结束后跳转至首页
知识点:
页面路由 (@ohos.router)
页面路由指在应用程序中实现不同页面之间的跳转和数据传递。Router模块通过不同的url地址,可以方便地进行页面路由,轻松地访问不同的页面。本文将从页面跳转、页面返回、页面返回前增加一个询问框和命名路由这几个方面,介绍如何通过Router模块实现页面路由。
Router模块提供了两种跳转模式,分别是router.pushUrl和router.replaceUrl。这两种模式决定了目标页面是否会替换当前页。
- router.pushUrl:目标页面不会替换当前页,而是压入页面栈。这样可以保留当前页的状态,并且可以通过返回键或者调用router.back方法返回到当前页。
- router.replaceUrl:目标页面会替换当前页,并销毁当前页。这样可以释放当前页的资源,并且无法返回到当前页。
setInterval定时器
setInterval(handler: Function | string, delay: number, ...arguments: any[]): number
重复调用一个函数,在每次调用之间具有固定的时间延迟。
删除该定时器需手动调用clearInterval接口。
clearInterval
clearInterval(intervalID?: number): void
可取消通过setInterval()设置的重复定时任务。
定时器对象保存在创建它的线程内,删除定时器需要在创建该定时器的线程删除。
实战:
LaunchPage
import { router } from '@kit.ArkUI';
@Entry
@Component
struct LaunchPage {
timer: number = 0
@State time: number = 5
onPageShow(): void {
this.timer = setInterval(() => {
this.time--;
if (this.time === 0) {
clearInterval(this.timer); // 关闭定时器
router.replaceUrl({ url: 'pages/44LaunchPage/IndexPage' });
}
}, 1000)
}
build() {
Column({ space: 10 }) {
Row({ space: 10 }) {
Text(`${this.time}秒后自动关闭`) //倒计时
Button('关闭', { type: ButtonType.Capsule, stateEffect: true })
.borderRadius(6)
.backgroundColor(Color.Gray)
.onClick(() => {
clearInterval(this.timer); // 关闭定时器
router.replaceUrl({ url: 'pages/44LaunchPage/IndexPage' });
})
}
.width('100%')
.justifyContent(FlexAlign.End)
Text('启动页实战')
.id('LaunchPageHelloWorld')
.fontSize(20)
.fontWeight(FontWeight.Bold)
Text('通常,启动页为企业广告')
Text('建议:启动页要有手动关闭的按钮')
Text('建议:最好不要有启动页广告,这样会更友好')
}
.width('100%')
.height('100%')
.alignItems(HorizontalAlign.Start)
.padding({ right: 20, left: 20 })
}
}
EntryAbility
import { AbilityConstant, ConfigurationConstant, UIAbility, Want } from '@kit.AbilityKit';
import { hilog } from '@kit.PerformanceAnalysisKit';
import { window } from '@kit.ArkUI';
const DOMAIN = 0x0000;
export default class EntryAbility extends UIAbility {
onCreate(want: Want, launchParam: AbilityConstant.LaunchParam): void {
this.context.getApplicationContext().setColorMode(ConfigurationConstant.ColorMode.COLOR_MODE_NOT_SET);
hilog.info(DOMAIN, 'testTag', '%{public}s', 'Ability onCreate');
}
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/44LaunchPage/LaunchPage', (err) => {
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');
}
}
IndexPage
@Entry
@Component
struct IndexPage {
@State message: string = '启动页实战-首页';
build() {
Column() {
Text(this.message)
.id('IndexPageHelloWorld')
.fontSize(30)
.fontWeight(FontWeight.Bold)
}
.height('100%')
.width('100%')
}
}
點(diǎn)擊查看更多內(nèi)容
為 TA 點(diǎn)贊
評(píng)論
評(píng)論
共同學(xué)習(xí),寫下你的評(píng)論
評(píng)論加載中...
作者其他優(yōu)質(zhì)文章
正在加載中
感謝您的支持,我會(huì)繼續(xù)努力的~
掃碼打賞,你說多少就多少
贊賞金額會(huì)直接到老師賬戶
支付方式
打開微信掃一掃,即可進(jìn)行掃碼打賞哦