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;
    }
  }
}
分享
微博
QQ
微信
回复
2024-12-25 15:42:17
相关问题
HarmonyOS 应用后台问题
561浏览 • 1回复 待解决
HarmonyOS是否有类似后台快照机制
278浏览 • 1回复 待解决
TextInput组件的onBlur、onFocus触发
926浏览 • 1回复 待解决
HarmonyOS 使用全局 @Builder 应用 crash
46浏览 • 1回复 待解决
HarmonyOS 应用全局弹框
536浏览 • 1回复 待解决
HarmonyOS 应用进入后台,提示用户
229浏览 • 1回复 待解决
HarmonyOS 应用手动退到后台
218浏览 • 1回复 待解决
HarmonyOS 如何实现应用全局换肤功能
86浏览 • 1回复 待解决
HarmonyOS VPN应用如何保持后台运行?
201浏览 • 1回复 待解决
HarmonyOS 后台应用列表模糊问题
84浏览 • 1回复 待解决