HarmonyOS 关于APP冷启动时检测到代理后的两种处理方案

APP在冷启动时,检测到手机设置了网络代理,此时基于当前窗口创建子窗户,并添加自定义弹窗提示,但随着APP执行onWindowStageCreate里面的其他方法,加载启动页还有其他Page页面,系统会将自定义弹窗移除掉,所以想借此问题,提出两个需求:

第一个需求,能否提供系统级别的弹窗加载Window的最上层,不会被后面的Page页面移除掉,一直在当前窗口最上层。

第二个需求,MainAbility里面是否可以增加一些生命周期方法,可以在对应方法里面提前检测到代理,然后等用户将APP切入后台,去关闭网络代理后,在将APP切回前台时,继续执行原来APP初始化的一些操作。

HarmonyOS
2024-12-26 16:26:49
浏览
收藏 0
回答 1
回答 1
按赞同
/
按时间
fox280
  1. 应用内弹窗应该优先用ark 的dialog组件。dialog组件之前有会随着应用page切换而消失的问题,后来已修复,可以由开发者控制。如果要一定用窗口的话,用子窗口就可以了,子窗口不会因为page切换消失的。如果主窗口退后台了,对应子窗口也会退后台。

  2. TYPE_SYSTEM_ALERT是一个全局最前的提示,用来做低电量提示,预警信息之类的,按照安全策略,不再开放

    可以在页面隐藏的时候根据业务是否销毁窗口;(不会出现子窗口被吞掉)

    示例代码如下:

import window from '@ohos.window';
import { BusinessError } from '@kit.BasicServicesKit';

@Entry
@Component
struct Index {
  @StorageLink('data') mainData: number = 1;
  @StorageLink('window') StorageWindow: window.WindowStage | null = null
  private windowStage = this.StorageWindow
  subWindowClass: window.Window | null = null;

  onPageHide(): void {
    this.destroySubWindow()
  }

  build() {
    Column({ space: 20 }) {
      TextInput()
        .fontSize(30)
        .fontWeight(FontWeight.Bold)

      Text('开启子窗口')
        .fontSize(30)
        .fontWeight(FontWeight.Bold)
        .onClick(() => {
          this.showSubWindow()
        })

      Text('关闭子窗口')
        .fontSize(30)
        .fontWeight(FontWeight.Bold)
        .onClick(() => {
          this.destroySubWindow()
        })
    }
    .width('100%')

  }

  async showSubWindow() {
    // 1.创建应用子窗口
    if (!this.windowStage) {
      console.error('Failed to create the subwindow. Cause: windowStage_ is null')
      return
    }
    this.windowStage.createSubWindow('mySubWindow', (err: BusinessError, data) => {
      if (err.code) {
        console.error('Failed to create the subwindow. Cause: ' + JSON.stringify(err))
        return
      }
      this.subWindowClass = data
      this.subWindowClass.keepKeyboardOnFocus(true)
      console.info('成功: ' + JSON.stringify(data))

      // 2.子窗口创建成功后,设置子窗口的位置、大小及相关属性等。
      this.subWindowClass.moveWindowTo(200, 800, (err) => {
        if (err.code) {
          console.error('Failed to move the window. Cause:' + JSON.stringify(err));
          return;
        }
        console.info('Succeeded in moving the window.');
      });

      this.subWindowClass.resize(700, 700, (err) => {
        if (err.code) {
          console.error('Failed to change the window size. Cause:' + JSON.stringify(err));
          return;
        }
        console.info('Succeeded in changing the window size.');
      });

      // 3.为子窗口加载对应的目标页面。
      this.subWindowClass.setUIContent("pages/WindowPage", (err) => {
        if (err.code) {
          console.error('Failed to load the content. Cause:' + JSON.stringify(err));
          return;
        }
        console.info('Succeeded in loading the content.');
        // 3.显示子窗口。
        // (this.sub_windowClass as window.Window).setWindowFocusable(false);
        (this.subWindowClass as window.Window).showWindow((err) => {
          if (err.code) {
            console.error('Failed to show the window. Cause: ' + JSON.stringify(err));
            return;
          }
          console.info('Succeeded in showing the window.');
        });
      });
    })

  }

  destroySubWindow() {
    // 4.销毁子窗口。当不再需要子窗口时,可根据具体实现逻辑,使用destroy对其进行销毁。
    this.subWindowClass && (this.subWindowClass as window.Window).destroyWindow((err) => {
      if (err.code) {
        console.error('Failed to destroy the window. Cause: ' + JSON.stringify(err));
        return;
      }
      console.info('Succeeded in destroying the window.');
    });
  }
}
  • 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.
  • 75.
  • 76.
  • 77.
  • 78.
  • 79.
  • 80.
  • 81.
  • 82.
  • 83.
  • 84.
  • 85.
  • 86.
  • 87.
  • 88.
  • 89.
  • 90.
  • 91.
  • 92.
  • 93.
  • 94.
  • 95.
  • 96.
  • 97.
  • 98.
  • 99.
  • 100.
  • 101.
  • 102.
  • 103.
分享
微博
QQ
微信
回复
2024-12-26 17:53:48
相关问题
HarmonyOS 冷启动时启动实现
1041浏览 • 1回复 待解决
HarmonyOS 两种模式布局如何兼容。
1457浏览 • 1回复 待解决
HarmonyOS App启动时动画怎么取消
1013浏览 • 1回复 待解决
app启动时加在so库crash
1515浏览 • 1回复 待解决
HarmonyOS 是否能检测到app签名
819浏览 • 1回复 待解决
PolarDB 集群连接地址包括哪两种
3789浏览 • 1回复 待解决
HarmonyOS App启动时间统计
1305浏览 • 1回复 待解决
HarmonyOS Profiler 工具分析 APP 冷启动
922浏览 • 1回复 待解决
js获取canvas对象两种方式有啥不同?
8689浏览 • 1回复 待解决
HarmonyOS 状态更新没有检测到
965浏览 • 1回复 待解决
提问
该提问已有0人参与 ,帮助了0人