HarmonyOS 全局触摸应用切后台未触发

windowClass.on(‘noInteractionDetected’, 60, callback) 超时时间内无交互事件监听,应用切到后台之后一直没法触发。

HarmonyOS
2024-12-25 13:39:46
浏览
收藏 0
回答 1
回答 1
按赞同
/
按时间
superinsect

示例参考如下:

import { window } from '@kit.ArkUI';

@Entry
@Component
struct AutoLogoutApp {
  @State isLoggedIn: boolean = false;
  @State timeoutId: number | null = null;
  @State idleTimeout: number = 5000; // 设置闲置时间为60秒
  @State message: string = '';
  windowClass: window.Window | undefined = undefined;

  onPageHide(): void {
    console.log('hide')
  }

  build() {
    Column() {
      if (!this.isLoggedIn) {
        Button('Login')
          .onClick(() => {
            this.login();
          });
      } else {
        Text('You are logged in. Interact to stay logged in.')
          .fontSize(20);
        Button('Logout')
          .onClick(() => {
            this.logout();
          });
      }
      if (this.message) {
        Text(this.message)
          .fontSize(16)
          .fontColor('#ff0000');
      }
    }
    .justifyContent(FlexAlign.Center)
    .width('100%')
    .height('100%')
    // 监听触摸事件,重置闲置计时器
    .onTouch(() => {
      this.resetIdleTimer();
    });
  }

  login() {
    this.isLoggedIn = true;
    this.message = '';
    this.resetIdleTimer();
  }

  logout() {
    this.isLoggedIn = false;
    this.clearIdleTimer();
    this.message = 'You have been logged out due to inactivity.';
  }

  resetIdleTimer() {
    if (this.timeoutId !== null) {
      clearTimeout(this.timeoutId);
    }
    this.timeoutId = setTimeout(() => {
      console.log('idleTimeout')
      this.logout();
    }, this.idleTimeout);
  }

  clearIdleTimer() {
    if (this.timeoutId !== null) {
      clearTimeout(this.timeoutId);
      this.timeoutId = null;
    }
  }
}
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.
  • 23.
  • 24.
  • 25.
  • 26.
  • 27.
  • 28.
  • 29.
  • 30.
  • 31.
  • 32.
  • 33.
  • 34.
  • 35.
  • 36.
  • 37.
  • 38.
  • 39.
  • 40.
  • 41.
  • 42.
  • 43.
  • 44.
  • 45.
  • 46.
  • 47.
  • 48.
  • 49.
  • 50.
  • 51.
  • 52.
  • 53.
  • 54.
  • 55.
  • 56.
  • 57.
  • 58.
  • 59.
  • 60.
  • 61.
  • 62.
  • 63.
  • 64.
  • 65.
  • 66.
  • 67.
  • 68.
  • 69.
  • 70.
  • 71.
  • 72.
  • 73.
  • 74.
分享
微博
QQ
微信
回复
2024-12-25 15:42:17
相关问题
HarmonyOS 应用后台问题
1037浏览 • 1回复 待解决
HarmonyOS是否有类似后台快照机制
888浏览 • 1回复 待解决
TextInput组件的onBlur、onFocus触发
1435浏览 • 1回复 待解决
HarmonyOS 应用进入后台,提示用户
689浏览 • 1回复 待解决
HarmonyOS 应用手动退到后台
840浏览 • 1回复 待解决
HarmonyOS 使用全局 @Builder 应用 crash
764浏览 • 1回复 待解决
HarmonyOS 应用全局弹框
1187浏览 • 1回复 待解决
HarmonyOS 后台应用列表模糊问题
542浏览 • 1回复 待解决
HarmonyOS 如何实现应用全局换肤功能
738浏览 • 1回复 待解决