Android Eventbus Skill Point Grooming

Source: Internet
Author: User

Eventbus is an open source project on GitHub, address: Https://github.com/greenrobot/EventBus

Question: 1. Now all the projects created by Android Studio, how to import these items (unfamiliar with tools); 2. How do I get the. jar package for these open source projects?

Eventbus Conceptual analysis (get perceptual knowledge):

The publisher is Publisher, Subscriber is the Subscriber, and publisher of event is the owner of the events, and each Subscriber receives the corresponding event.

Its main function is to replace intent, Handler, broadcast to pass messages between fragment, Activity, service, and threads.

The advantage: Low overhead (jar ~50kb), code elegance, code runs efficiently, multiple APK tests ...

Eventbus Use the basic process:

Example analysis: Click on the button in the eventbusactivity, jump to the Activitysecond interface, and in the above interface, click the button to jump to the Activitythird interface, and enter the user name and password in the above interface, click Jump, Return to eventbusactivity and display the user name and password passed in.

Package Com.demo;import Android.app.activity;import Android.content.intent;import android.os.bundle;import Android.view.view;import Android.widget.textview;import De.greenrobot.event.eventbus;public class EventBusActivity    Extends Activity {private TextView mtextview;        @Override protected void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate);        Setcontentview (R.layout.activity_main);        Mtextview = (TextView) Findviewbyid (R.id.text); To determine if you have already registered, avoid re-registering if (! Eventbus.getdefault (). isregistered (This)) {//register for monitoring, write as far as possible after each variable initialization, avoid causing null pointer exception Eventbus.getdefault (). reg        Ister (this);     }} public void Btnclick (view view) {startactivity (new Intent (Eventbusactivity.this, Activitysecond.class));  }/** * < function description > Subscriber Receive event * * @param event [parameter description] * @return void [return type description] */public void           Oneventmainthread (Messageevent event) {if (event.getuser () = null) { Mtextview.settext ("User name:" + Event.getuser (). GetUserName () + ";        Password: "+ event.getuser (). GetPassword ());        }} @Override protected void OnDestroy () {Super.ondestroy ();    Unregister needs to be written in Ondestory (), written in OnStop () that may throw an exception eventbus.getdefault (). Unregister (this); }}

Activitysecond.java code Example:

Package Com.demo;import Android.app.activity;import Android.content.intent;import android.os.bundle;import Android.view.view;public class Activitysecond extends Activity {    @Override    protected void OnCreate (Bundle Savedinstancestate) {        super.oncreate (savedinstancestate);        Setcontentview (r.layout.activity_main2);    }    public void BtnClick2 (view view) {        startactivity (new Intent (Activitysecond.this, Activitythird.class));        Finish ();    }}

Activitythird.java code Example:

Package Com.demo;import Android.app.activity;import Android.os.bundle;import android.view.view;import Android.widget.edittext;import De.greenrobot.event.eventbus;public class Activitythird extends Activity {    Private EditText meditusername;    Private EditText meditpwd;    @Override    protected void onCreate (Bundle savedinstancestate) {        super.oncreate (savedinstancestate);        Setcontentview (r.layout.activity_main3);        Meditusername = (EditText) Findviewbyid (r.id.edit_username);        Meditpwd = (EditText) Findviewbyid (R.ID.EDIT_PWD);    }    public void BtnClick3 (view view) {        String userName = Meditusername.gettext (). toString (). Trim ();        String password = Meditpwd.gettext (). toString (). Trim ();        Send Message        eventbus.getdefault (). Post (                new Messageevent (New User (userName, password));        Finish ();    }}

Eventbus Precautions:

The four methods involved in receiving a message, as well as the distinction:

1. OnEvent (): The event on which thread is published, OnEvent () will run in this thread. That is, the publish event and receive event threads are on the same thread. With this approach, time-consuming operations cannot be performed in OnEvent (), which can lead to delays in event distribution if the execution of time-consuming operations is easy.

2. Oneventmainthread (): Regardless of which thread the event is published in, Oneventmainthread executes in the UI thread, and the receive event runs in the UI thread. This is very useful for Android, because the UI can only be updated in the UI thread in Android, so time-consuming operations cannot be performed in the method.

3. Oneventbackground (): If the event is published from the UI thread, then Oneventbackground runs in the child thread that was created, and if the event itself is published in a child thread, it runs directly within that child thread. 、

4. Oneventasync (): Regardless of which thread the event was published in, a new child thread is created to execute the method.

If there are multiple places to publish events, there are multiple places to receive and handle events, how to match and judge?

According to the above 4 methods to receive the event parameters, and parameter matching; The parameter that sends the message is a class, and the received must be this class, otherwise it will not be received. If more than one onevent () has the same parameters, these methods will receive this message.

Android Eventbus Skill Point Grooming

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.