5

I am creating a custom component in HarmonyOS using Java SDK, Where I have to perform some task after some delay with Component instance.

In Android, we have postDelayed(Runnable action, long delayMillis) method in View class. So we can achieve above requirement as follow

view.postDelayed(runnable, 100);

but, In HMOS java SDK, I seen there is no any api available for delay in Component class.

So, My question is

What is the equivalent of view.postDelayed(Runnable action, long delayMillis) in harmonyos ?

zhangxaochen
  • 32,744
  • 15
  • 77
  • 108
Shivam Jamaiwar
  • 524
  • 1
  • 4
  • 14

2 Answers2

2

currently HarmonyOS doesn't provide exact alternative for View.postDelayed(..) , Instead you can achieve similar UI post functionality using ohos.eventhandler.EventHandler API, Sample Usage is as follows

 EventHanlder eventhandler = new EventHandlder(EventRunner.getMainEventRunner());
 eventhandler.postTask(runnable, timeInMillis);
Gowtham GS
  • 478
  • 2
  • 5
  • 2
    **note:** If you create EventRunner instance like this way `EventRunner eventRunner = EventRunner.create();` then, your app will crashed with **Attempt to update UI in non-UI thread** error. So please do it carefully. May be this comment helpful for others. – Shivam Jamaiwar Sep 06 '21 at 06:58
  • How to do that in smart devices, so JS not Java? – lacas Jan 25 '22 at 07:47
2

You could also refer to the following code:

getUITaskDispatcher().delayDispatch(new Runnable() {

    @Override

    public void run() {

        // Here's the task you want to delay.

    }

}, 100);
zhangxaochen
  • 32,744
  • 15
  • 77
  • 108