1

I wanted to do some task in the background like in android we can use AsyncTask to do some work using UI thread, in Harmony we have EventHandler which allows us to send and process InnerEvent and Runnable objects on asynchronous threads.

I just want a simple example on how to use it.

Joachim Sauer
  • 302,674
  • 57
  • 556
  • 614
Raj Bhagat
  • 81
  • 5

2 Answers2

2

please check the sample -

 public class EventHandlerImplementation extends EventHandler {
        private EventHandlerImplementation(EventRunner runner) {
            super(runner);
        }

        @Override
        public void processEvent(InnerEvent event) {
            getUITaskDispatcher().asyncDispatch(() -> {
                 // do your stuff here
            });
        }
    }
Gowtham GS
  • 478
  • 2
  • 5
Kanak Sony
  • 1,570
  • 1
  • 21
  • 37
1
private final int eventUpdateGet = 1001;
private final int eventUpdateSend = 1002; 

private class MyEventHandler extends EventHandler {
    private MyEventHandler(EventRunner runner) throws IllegalArgumentException {
        super(runner);
    }

    @Override
    protected void processEvent(InnerEvent event) {
        super.processEvent(event);
        switch (event.eventId) {
            case eventUpdateGet:
                Object object = event.object;
                txGet.setText(String.valueOf(object));
                break;
            case eventUpdateSend:
                ....
                break;
            default:
                break;
        }
    }
}


@Override
protected void onStart(Intent intent) {
    myHandler = new MyEventHandler(EventRunner.current());
}

When you use, you could :

String msgGet = "......"
InnerEvent event = InnerEvent.get(eventUpdateGet, msgGet);
myHandler.sendEvent(event);

For more details, pls kindly refer to this official Docs.

zhangxaochen
  • 32,744
  • 15
  • 77
  • 108