HarmonyOS踩坑记——postTask为什么不执行

1,364 阅读1分钟

本着新技术要第一时间尝试的原则,今天尝试了一下鸿蒙OS,初探即踩坑。

今天想在AbilitySlice里实现一个延时的操作,按照Android的原则,我在AbilitySlice里面找到了postTask方法,结果发现完全不执行。

        postTask(()->{
            new ToastDialog(this).setText("演示操作").setDuration(3000).show();
        },3000L);

看了半天代码逻辑并没有问题,但代码确实没有执行。

于是翻了半天的文档,找到了如下说明

This method can only be used to perform component-related time-consuming tasks such as initializing the component tree in onStart(ohos.aafwk.content.Intent), onActive(), or onStop() of a Page ability. Additionally, you must override the supportHighPerformanceUI() method. Do not use this method to execute other time-consuming operations such as I/O and network processing.

简单翻译一下就是这个方法只能被用来执行组件相关的耗时操作,不能用来执行IO和网络等耗时的操作,如果使用的话需要重新supportHighPerformanceUI方法。

而supportHighPerformanceUI方法的描述如下

Checks whether this ability supports high-performance UI rendering. You should override this method and make it return true if you want your ability to support high-performance UI rendering.

于是我重写了supportHighPerformanceUI并返回了true

    @Override
    public boolean supportHighPerformanceUI() {
        return true;
    }

果然生效。

现在看不了鸿蒙的详细的Java代码,翻文档真的太累了。