"Android Advanced Light"--event bus

Source: Internet
Author: User

NO1:

Eventbus three elements:

1) Event: Events

2) Subscriber: Event subscribers

3) Publisher: Event Publisher

No2:

Eventbus 4 Types of Threadmode (threading model):

1) POSTING (default): Which thread the event is published on, and in which thread the event handler will run

2) MAIN: Handling of events is performed in the UI thread

3) BACKGROUND: Execute in New thread or sub-thread released, disable UI update operation

4) ASYNC: Execute in New child thread, disable UI update operation

No3:

Eventbus Source Code Analysis

Eventbus.getdefault () (singleton mode)

New Eventbus ()

Eventbus (eventbusbuilder) (builder mode)

Register->

Subscribermethodfinder.findsubscribermethods->findusinginfo->getsubscriberinfo-> Findusingreflectioninsingleclass->

Subscribe->

...

No4:

Otto Use

Compile ' com.squareup:otto:1.3.8 '
 Public class busdata{    public  String message;      Public busdata (String message) {        this. Message = message;    }       Public String getMessage () {        return  message;    }      Public void setmessage (String message) {        this. Message = message;    }} 
 Public classOttobusextendsbus{Private volatile StaticOttobus Bus; PrivateOttobus () {} Public StaticOttobus getinstance () {if(Bus = =NULL){            synchronized(Ottobus.class){                if(bus==NULL) {Bus=NewOttobus (); }            }        }        returnbus; }}
Bus bus = ottobus.getinstance (); Bus.register (this), Bus.unregister (this); @ Subscribepublicvoid  setcontent (busdata data) {    Tv_message.settext (data.getmessage ( ));} Ottobus.getinstance (). Post (new busdata ("Liu Wangshu's Blog updated"));

NO5:

Functions of Otto Main class

1) Produce, Subscribe: Publisher and Subscriber note classes

2) Bus: Event Bus class, used to register and unregister, maintain publish-subscribe model, and handle event dispatch distribution

3) Handlerfinder, Annotatedhandlerfinder: Used to find publishers and Subscribers

4) Eventproducer, EventHandler: Encapsulates the data structure of the Publisher and subscriber separately

"Android Advanced Light"--event bus

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.