What is more efficient Broadcast Receiver or Handler?

后端 未结 3 1970
悲哀的现实
悲哀的现实 2021-02-01 07:49

I know that onReceive() of the Broadcast receiver and handleMessage() of Handler run on the same UI thread. Suppose I want to communicate between two services, within the same a

3条回答
  •  面向向阳花
    2021-02-01 08:17

    Broadcast receiver calls are heavy operations and there are chances to get ANR's if an event is broadcasted for multiple times. And also the context you get in onReceive() of Broadcast receiver has limited usage until you get application context.

    In contrast, handler calls are efficient as they are simple and runs in the different thread and no context is required to start a handler call. communicating between 2 services or activities or 2 threads can be easily handled using handler. Infact all the other ways viz.. Intents and Bound services use handlers internally for message passing.

提交回复
热议问题