Android third-party class library simple to use Eventbus

Source: Internet
Author: User
Tags eventbus

Android third-party class library Eventbus

1 PS

工欲善其事 its prerequisite. Eventbus is also a tool used in development.

This article is also a brief introduction and use of Eventbus, the same level as the previous xutils. http://www.cnblogs.com/greentomlee/p/6025470.html

Author: Xiu Yu Xuan Chen @ Blog Park

2 Introduction

Open Source Project Address:

Https://github.com/greenrobot/EventBus

The Eventbus is a publish/Subscribe event bus optimized for Android . The main function is to substitute intent,handler,broadcast for passing messages between Fragment,activity,service and threads. The advantage is that the cost is small and the code is more elegant. and decoupling the sender and receiver.

Eventbus Publish Subscription (Publish/subscribe)

Eventbus has the following advantages:

· Simplifies the communication between components

    • Decouples Event Senders and receivers
    • Performs well with activities, fragments, and background threads
    • Avoids complex and error-prone dependencies and life cycle issues

· Makes your code simpler (can be simplified for programmers)

· is fast (execution speed is fast)

· Is tiny (~50k jar) (class library is small in size, about 50k)

· is proven in practice by apps with 100,000,000+ installs

· Have advanced features like delivery threads, subscriber priorities, etc.

3 Environment Building Trilogy 3.1 Resources download

Download the Open source class library locally on GitHub:

Https://github.com/greenrobot/EventBus

The author provides a very detailed description of the document, I am referring to the author's document written out. There are also simple steps to use on the GitHub home page.

http://greenrobot.org/eventbus/documentation/how-to-get-started/

3.2 Project Environment Construction

-------------

Software Environment: Android Studio2.3 Beta3

Eventbus downloaded from GitHub

----------------

First, create a new test project: Testeventbus

Then we look at the directory structure of the downloaded Eventbus and determine which module is to be imported

By analyzing the structure in the catalog, we know:

Under Eventbus-master There are 3 modules that we can import into the project. respectively: Eventbus, Eventbusannotationprocessor, Eventbusperformance. According to the programming habit we know: The Eventbus directory should be imported.

Once you have identified the imported class library,

Create a new imported module under this project

Select the ' evenbus ' directory to import.

The structure after the import succeeds is as follows:

The next step is to edit the Build.gradle (note the directory where the Build.gradle file resides)

Write this phrase in the file:

Compile project (': Eventbus ')

This is because our module name is Eventbus

(Build.gradle The name of the dependent class library being written should be associated with the import Eventbus the module name after remain consistent)

3.3 Test use

First describe what we are going to do:

To compare the difference between the intent pass parameter and the event bus pass parameter, and to practice using Eventbus, we prepared two activity:mainactivity and sendmsgactivity

First, the message is sent from mainactivity to sendmsgactivity using the traditional way of message passing;

The Eventbus is then used to pass the new message from Sendmsgactivity to mainavtivity.

PS this part of using intent to pass the message is not repeating

3.3.1 Registering and unregistering messages where messages are received

First Look at mainactivity

Due to the need to receive evenbus messages from Sendmsgactivity, a new instance registration Eventbus is required, and the registered action should be written in the OnStart () callback function.

@Override
protected void OnStart () {//Register event Bus
Super.onstart ();
Eventbus. Getdefault (). Register (this);
}

Also need to log off when the file program is destroyed:

@Override
protected void OnDestroy () {//Logoff Eventbus
Super.ondestroy ();
Eventbus. Getdefault (). Unregister (this);
}
3.3.2 Write the sending method where the message is sent

Sendmsgacitivty will send a message using Eventbus, then he is where the message is sent.

The careful classmate discovered, I did not pass the parameter in intent inside. Instead, it uses

Ventbus. Getdefault (). Post ()

Method class to send a message. The key code is as follows:

Sendbtn.setonclicklistener (New View.onclicklistener () {
@Override
public void OnClick (view view) {
Eventbus. Getdefault (). Post (new Messageevent ("Come, send you a message ..."));

Intent Intent = new Intent (sendmsgactivity.this, Mainactivity.class);
StartActivity (Intent);

}
});

Where Messageevent is a custom entity (struct \bean)

public class Messageevent {
Public String msg= "";
Public messageevent (String msg)
{
this.msg = msg;
}

Public String getmsg () {
return msg;
}

public void Setmsg (String msg) {
this.msg = msg;
}
}
3.3.3 write the action of receiving the message where the message is received

The method to receive the message in the place where the message is accepted (mainactivity) is as follows:

/**
* Handling the method of receiving messages "subscriber methods"
* Annotations can also be used
* @link http://greenrobot.org/eventbus/documentation/how-to-get-started/
*
* @param evnt
*/
@Subscribe (Threadmode = Threadmode. MAIN)
public void Oneventmainthread (Messageevent evnt)
{
String msg = evnt.getmsg ();
THIS.SHOWMSGTV = (TextView) Findviewbyid (r.id.tv_printer);
This.showMsgTv.setText (msg);
Toast. Maketext (This, MSG, Toast.) Length_long). Show ();
}

The effect that appears:

4 Extended Reading

Use and comparison of Eventbus & Otto

Http://www.jianshu.com/p/cb39a0018db1

The difference between Rxjava and Eventbus?

https://www.zhihu.com/question/32179258

http://bbs.csdn.net/topics/392007033

Android third-party library easy to use Eventbus

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.