EventBus and androideventbus for simple use of Android third-party class libraries

Source: Internet
Author: User
Tags eventbus

EventBus and androideventbus for simple use of Android third-party class libraries

EventBus for third-party Android Class Libraries

1 PS

To do something better, you must first sharpen the tool. Eventbus is also a common tool in development.

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

Author: Xiu Yuxuan Chen @ blog Park

2 Introduction

Open Source Project address:

Https://github.com/greenrobot/EventBus

EventBus isAndroidOptimized publish/subscribe event bus. The main function is to replace Intent, Handler, and BroadCast to transmit messages between Fragment, Activity, Service, and threads. The advantage is that the overhead is small and the code is more elegant. And decouple the sender and receiver.

Publish and subscribe of Eventbus (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 (code can be simplified for programmers)

· Is fast (fast execution)

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

· Is proven in practice by apps with 100,000,000 + instils

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

3. Set up a trilogy and download 3.1 Resources

Download the Open Source class library to your local device on github:

Https://github.com/greenrobot/EventBus

The detailed instructions provided by the author are provided by reference to the author's documents. Simple instructions are also provided on the github homepage.

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 test project: TestEventBus

Then, observe the directory structure of the downloaded eventbus and determine the module to be imported.

By analyzing the structure in the directory, we know:

In EventBus-master, there are three modules that can be imported into the project. EventBus, EventBusAnnotationProcessor, and EventBusPerformance. According to programming habits, we know that the EventBus directory should be imported.

After confirming the imported class library,

Create a new imported module under this project

Select the 'evenbus' directory for import.

The structure after successful import is as follows:

Next, edit build. gradle (note the directory where the build. gradle file is located)

Write this statement into the file:

Compile project (': eventbus ')

This is because our module name is EventBus.

(Build. gradleThe name of the dependent class library to be written should be the same as that of the imported EventBusModule name)

3.3 test use

First, describe what we want to do:

To compare the differences between intent parameters and Event bus parameters, and to practice using EventBus, we have prepared two activities: MainActivity and SendMsgActivity.

First, the message is sent from MainActivity to SendMsgActivity using the traditional message transmission method;

Then, use Eventbus to pass the new message from SendMsgActivity to mainavti.pdf.

This part of the message transmitted by using intent is not described in detail.

3.3.1 register and deregister the message at the place where the message is received

First look at MainActivity

To receive the EvenBus message from SendMsgActivity, you need to create an instance to register eventBus. The registered action should be written in the onStart () callback function.

@ Override
Protected void onStart () {// register event bus
Super. onStart ();
EventBus.GetDefault(). Register (this );
}

At the same time, when the file program is destroyed, it also needs to be canceled:

@ Override
Protected void onDestroy () {// cancel eventBus
Super. onDestroy ();
EventBus.GetDefault(). Unregister (this );
}
3.3.2 write the sending method at the place where the message is sent

SendMsgAcitivty uses Eventbus to send a message, which is the place where the message is sent.

Careful students found that I didn't pass parameters in intent, but used

ventBus.getDefault().post()

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 );

}
});

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 message receiving action where the message is accepted

The method for receiving a message in MainActivity is as follows:

/**
* The method "subscriber methods" for processing received messages"
* You can also use annotations.
* @ 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 ();
}

Under the following results:

4. Additional reading

Use and comparison of EventBus & Otto

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

What is the difference between RxJava and EventBus?

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

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

Related Article

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.