HarmonyOS 如何能让loop的swiper能通知到使用方,令它的倒计时暂停。如应用去到后台或上面盖了页面时导致的暂停

HarmonyOS
1天前
浏览
收藏 0
回答 1
待解决
回答 1
按赞同
/
按时间
FengTianYa

参考示例如下:

//EntryAbility.ets
import { AbilityConstant, UIAbility, Want } from '@kit.AbilityKit';
import { hilog } from '@kit.PerformanceAnalysisKit';
import { window } from '@kit.ArkUI';

export default class EntryAbility extends UIAbility {
  onCreate(want: Want, launchParam: AbilityConstant.LaunchParam): void {
    hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onCreate');
  }

  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/Index', (err) => {
      AppStorage.setOrCreate<boolean>('video', true)
      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.');
    });
  }

  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
    AppStorage.Set("video", true)
    hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onForeground');
  }

  onBackground(): void {
    // Ability has back to background
    AppStorage.Set("video", false)
    hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onBackground');
  }
}
//index.ets
@Entry
@Component
struct Page1 {
  @State message: string = 'Hello World'
  @StorageLink('video') isOnForeground: boolean = true
  build() {
    Row() {
      Column() {
        Text(this.message)
          .fontSize(50)
          .fontWeight(FontWeight.Bold)
        Vid({ isOnForeground: this.isOnForeground })
      }
      .width('100%')
    }
    .height('100%')
  }
}

@Component
struct Vid {
  @Watch('change') @Link isOnForeground: boolean
  @State message: string = 'video'

  build() {
    Text("message")
      .fontSize(50)
      .fontWeight(FontWeight.Bold)
      .onClick(() => {
        this.message += this.isOnForeground
        console.log("" + this.isOnForeground)
      })
  }

  change() {
    if (this.isOnForeground) {
      console.log('组件在前台')
    } else {
      console.log('组件在后台')
    }
  }
}
分享
微博
QQ
微信
回复
1天前
相关问题
HarmonyOS 倒计时天数
341浏览 • 1回复 待解决
如何实现文本类型倒计时
923浏览 • 0回复 待解决
HarmonyOS 如何自动倒计时组件
473浏览 • 1回复 待解决
HarmonyOS TextTimer倒计时问题
14浏览 • 1回复 待解决
HarmonyOS 倒计时方法
32浏览 • 1回复 待解决
HarmonyOS 实现倒计时功能
56浏览 • 1回复 待解决
HarmonyOS 60秒倒计时demo
194浏览 • 1回复 待解决
HarmonyOS 请问ArkTS如何实现倒计时功能?
32593浏览 • 8回复 待解决
HarmonyOS Timer倒计时自定义
458浏览 • 1回复 待解决
如何实现一个倒计时器?
502浏览 • 1回复 待解决
HarmonyOS TextTimer60秒倒计时显示成00
32浏览 • 1回复 待解决
提问
该提问已有0人参与 ,帮助了0人