Accessing UI thread handler from a service

后端 未结 7 1530
被撕碎了的回忆
被撕碎了的回忆 2020-11-27 11:57

I am trying some thing new on Android for which I need to access the handler of the UI thread.

I know the following:

  1. The UI thread has its own handler
7条回答
  •  借酒劲吻你
    2020-11-27 12:29

    At the moment I prefer using event bus library such as Otto for this kind of problem. Just subscribe the desired components (activity):

    protected void onResume() {
        super.onResume();
        bus.register(this);
    }
    

    Then provide a callback method:

    public void onTimeLeftEvent(TimeLeftEvent ev) {
        // process event..
    }
    

    and then when your service execute a statement like this:

    bus.post(new TimeLeftEvent(340));
    

    That POJO will be passed to your above activity and all other subscribing components. Simple and elegant.

提交回复
热议问题