<span id="Label3"></p>1.EventBus Pros and cons<p><p>eventbus, as a common framework in Android development, has many advantages:</p></p> <ul> <ul> <li><p>Flexible scheduling. Do not rely on the context, the use of the same as the broadcast focus on the context of the injection and delivery. The parent Class's ability to listen to and handle notifications can be inherited to subclasses, which is critical for simplifying the code, the priority of notifications, the ability to keep subscriber focused on the most important notifications, and the sticky event (sticky events) to ensure that notifications are not ignored by subscriber. inheritance, priority, and stickiness are the greatest advantages of eventbus than broadcasting and observers, which make it possible to create a well-organized notification System.</p></li> <li><p>Easy to Use. Eventbus Subscriber Registration is very simple, call the Eventbus object's register method, If you do not want to create Eventbus can also call the static method Eventbus.getdefault () Get the default instance, SUBSC Riber the operation after receiving the notification is put in the OnEvent method is Ok. The process of becoming Publisher is much simpler, just call the appropriate Eventbus (either your own or the Default) post Method.</p></li> <li><p>Fast and Light weight. As one of Github's star projects, Eventbus has many tricks in the source code to improve performance;</p></li> </ul> </ul> <ul> <ul> <li>The disadvantage of Eventbus is that his code logic is not very clear, when subscriber registers, the method in Subscriber is traversed to find the public method beginning with OnEvent. This brings up some problems, and once the code is confused, it can't be found. The good news is that Eventbus already intends to use annotations to implement this, which should solve the problem of code Obfuscation.<br>But one drawback is that the observer is unique, and that is, the observer may cause an expansion of the Interface. Especially when the program requires a large number of different forms of notification, and the programmer has not made a good abstraction, the code will contain a large number of interfaces, the increase in the number of interfaces will bring a lot of naming, comments, and so On. essentially, The Observer requires the programmer to implement the event generation, distribution, and processing from scratch, which requires the participant to have a good understanding of the entire notification process. This is a reasonable requirement when the program code is appropriate, but it becomes a burden when the program is too large.</li> </ul> </ul><p><p><br></p></p><p><p>Below we achieve these one by one effects, a page jumps to B page, b page Send notification update a page of the TextView font color (color value random), as Follows:</p></p><p><p></p></p>2. Project Structure<p><p></p></p>3.activity_main.xml<p><p>Put a button and a display text textview, jump to nextactivity button</p></p><pre class="prettyprint"><code class=" hljs xml"><span class="hljs-tag"><span class="hljs-tag"><<span class="hljs-title">linearlayout </span> <span class="hljs-attribute">xmlns: Android </span> =<span class="hljs-value"> "http://schemas.android.com/apk/res/android" </span> <span class=" Hljs-attribute ">xmlns:tools </span> =<span class=" hljs-value ">" http://schemas.android.com/tools "</span> <span class="hljs-attribute">android:layout_width </span> =<span class="hljs-value"> "match_parent" </span> <span class=" Hljs-attribute ">android:layout_height </span> =<span class=" hljs-value ">" match_parent "</span> <span class=" Hljs-attribute ">android:orientation </span> =<span class=" hljs-value ">" Vertical "</span> <span class="hljs-attribute">android:gravity </span> =<span class="hljs-value"> "center" </span> <span class=" Hljs-attribute ">tools:context </span> =<span class=" hljs-value ">" ${relativepackage}.${activityclass} "</span> > </span></span> <span class="hljs-tag"><span class="hljs-tag"><<span class="hljs-title">button </span> <span class="hljs-attribute">android:id </span> =<span class="hljs-value"> "@+id/btn" </span> <span class="hljs-attribute">android:layout_width </span> =< Span class= "hljs-value" > "wrap_content" </span> <span class="hljs-attribute">android:layout_height </span> =<span class="hljs-value"> "wrap_content" </span> <span class="hljs-attribute">android:textsize </span> =<span class=" Hljs-value ">" 16sp "</span> <span class=" hljs-attribute ">android:text </span> =<span class=" hljs-value ">" Jump Nextactivity "</span>/> </span> <span class="hljs-tag"><span class="hljs-tag"><<span class="hljs-title">TextView</span><span class="hljs-attribute">android:id</span>= "<span class="hljs-value">@+id/color_text"</span><span class="hljs-attribute">android:layout_width</span>=<span class="hljs-value">" Match_parent "</span><span class="hljs-attribute">android:layout_height</span>=<span class="hljs-value">" Wrap_content "</span><span class="hljs-attribute">android:layout_margintop</span>= <span class="hljs-value"> "20dp"</span><span class="hljs-attribute">android:textsize</span>=<span class="hljs-value">"18sp"</span><span class="hljs-attribute">android:text</span>=<span class="hljs-value">"this is a text that can be changed by the font color"</span> / > </span></span><span class="hljs-tag"><span class="hljs-tag"></<span class="hljs-title">linearlayout</span>></span></span></code></pre>4.changetextcolorevent.java<p><p>Define the event class, get the color and current activity</p></p><pre class="prettyprint"><code class=" hljs java"><span class="hljs-javadoc"><span class="hljs-javadoc">/** * * < change the color of event> * *<span class="hljs-javadoctag"> @author</span> Wangkeke *<span class="hljs-javadoctag"> @version</span> [V1.00, May 10, 2016] *<span class="hljs-javadoctag"> @see</span> [related classes/methods] *<span class="hljs-javadoctag"> @since</span> V1.00 * *</span></span><span class="hljs-keyword"><span class="hljs-keyword"></span> public</span> <span class="hljs-class"><span class="hljs-class"> <span class="hljs-keyword">class</span> <span class="hljs-title">changetextcolorevent</span>{</span></span> <span class="hljs-keyword"><span class="hljs-keyword">Private</span></span>String color;<span class="hljs-keyword"><span class="hljs-keyword">Private</span></span>Activity activity;<span class="hljs-keyword"><span class="hljs-keyword"></span> public</span> <span class="hljs-title"><span class="hljs-title">changetextcolorevent</span></span>(String color, Activity Activity) {<span class="hljs-keyword"><span class="hljs-keyword">Super</span></span>();<span class="hljs-keyword"><span class="hljs-keyword"></span> this</span>. color = color;<span class="hljs-keyword"><span class="hljs-keyword"></span> this</span>. Activity = activity; }<span class="hljs-keyword"><span class="hljs-keyword"></span> public</span>String<span class="hljs-title"><span class="hljs-title">GetColor</span></span>() {<span class="hljs-keyword"><span class="hljs-keyword">return</span></span>Color }<span class="hljs-keyword"><span class="hljs-keyword"></span> public</span>Activity<span class="hljs-title"><span class="hljs-title">getactivity</span></span>() {<span class="hljs-keyword"><span class="hljs-keyword">return</span></span>Activity }}</code></pre>5.mainactivity.java<p><p>The code is very simple, register eventbus, unregister eventbus,oneventmainthread to receive subscription events</p></p><pre class="prettyprint"><code class=" hljs java"><span class="hljs-keyword"><span class="hljs-keyword"></span> public</span> <span class="hljs-class"><span class="hljs-class"> <span class="hljs-keyword">class</span> <span class="hljs-title">mainactivity</span> <span class="hljs-keyword">extends</span> <span class="hljs-title">Activity</span>{</span></span> <span class="hljs-keyword"><span class="hljs-keyword">Private</span></span>Button button;<span class="hljs-keyword"><span class="hljs-keyword">Private</span></span>TextView TextView;<span class="hljs-annotation"><span class="hljs-annotation">@Override</span></span> <span class="hljs-keyword"><span class="hljs-keyword">protected</span></span> <span class="hljs-keyword"><span class="hljs-keyword">void</span></span> <span class="hljs-title"><span class="hljs-title">onCreate</span></span>(Bundle Savedinstancestate) {<span class="hljs-keyword"><span class="hljs-keyword">Super</span></span>. onCreate (savedinstancestate); Setcontentview (r.layout.activity_main);<span class="hljs-comment"><span class="hljs-comment">//register Eventbus</span></span>Eventbus.getdefault (). Register (<span class="hljs-keyword"><span class="hljs-keyword"></span> this</span>); Initview (); }<span class="hljs-keyword"><span class="hljs-keyword">Private</span></span> <span class="hljs-keyword"><span class="hljs-keyword">void</span></span> <span class="hljs-title"><span class="hljs-title">Initview</span></span>() {button = (button) Findviewbyid (r.id.btn); TextView = (textView) Findviewbyid (r.id.color_text); Button.setonclicklistener (<span class="hljs-keyword"><span class="hljs-keyword">New</span></span>Onclicklistener () {<span class="hljs-annotation"><span class="hljs-annotation">@Override</span></span> <span class="hljs-keyword"><span class="hljs-keyword"></span> public</span> <span class="hljs-keyword"><span class="hljs-keyword">void</span></span> <span class="hljs-title"><span class="hljs-title">OnClick</span></span>(View V) {<span class="hljs-comment"><span class="hljs-comment">//jump to Nextactivity</span></span>Intent Intent =<span class="hljs-keyword"><span class="hljs-keyword">New</span></span>Intent (mainactivity.<span class="hljs-keyword"><span class="hljs-keyword"></span> this</span>, nextactivity.class); StartActivity (intent); } }); }<span class="hljs-javadoc"><span class="hljs-javadoc">/** * Public modifier, cannot use static abstart modifier * parameter can only have one *<span class="hljs-javadoctag"> @param</span> event *<span class="hljs-javadoctag"> @see</span> [class, class # method, class # member] */</span></span> <span class="hljs-annotation"><span class="hljs-annotation">@Subscribe</span></span> <span class="hljs-keyword"><span class="hljs-keyword"></span> public</span> <span class="hljs-keyword"><span class="hljs-keyword">void</span></span> <span class="hljs-title"><span class="hljs-title">Oneventmainthread</span></span>(changetextcolorevent Event) {textview.settextcolor (color.parsecolor (event.getcolor ()));<span class="hljs-comment"><span class="hljs-comment">//finish nextactivity</span></span>Event.getactivity (). Finish (); }<span class="hljs-annotation"><span class="hljs-annotation">@Override</span></span> <span class="hljs-keyword"><span class="hljs-keyword">protected</span></span> <span class="hljs-keyword"><span class="hljs-keyword">void</span></span> <span class="hljs-title"><span class="hljs-title">OnDestroy</span></span>() {<span class="hljs-keyword"><span class="hljs-keyword">Super</span></span>. OnDestroy ();<span class="hljs-comment"><span class="hljs-comment">//anti-registration Eventbus</span></span>Eventbus.getdefault (). Unregister (<span class="hljs-keyword"><span class="hljs-keyword"></span> this</span>); }}</code></pre>6.activity_next.xml<p><p>Define a button to send a message</p></p><pre class="prettyprint"><code class=" hljs xml"><span class="hljs-tag"><span class="hljs-tag"><<span class="hljs-title">relativelayout</span> <span class="hljs-attribute">xmlns:android</span>=<span class="hljs-value">"http://schemas.android.com/apk/res/android"</span> <span class="hljs-attribute">xmlns:tools</span>= "<span class="hljs-value">http://schemas.android.com/tools"</span><span class="hljs-attribute">android:layout_width</span>=<span class="hljs-value">"match_ Parent "</span><span class="hljs-attribute">android:layout_height</span>="<span class="hljs-value">match_parent "</span><span class="hljs-attribute">tools:context</span>=<span class="hljs-value">" ${ relativepackage}.${activityclass} "</span> > </span></span> <span class="hljs-tag"><span class="hljs-tag"><<span class="hljs-title">button</span><span class="hljs-attribute">android:id</span>=<span class="hljs-value">"@+id/button"</span><span class="hljs-attribute">android:layout_width</span>=<span class="hljs-value">"wrap_ Content "</span><span class="hljs-attribute">android:layout_height</span>=<span class="hljs-value">" Wrap_content "</span><span class="hljs-attribute">android:layout_centerinparent</span>= <span class="hljs-value"> "true"</span><span class="hljs-attribute">android:textsize</span>=<span class="hljs-value">"16sp"</span><span class="hljs-attribute">android:text</span>=<span class="hljs-value">"send event changing font color"</span> / > </span></span><span class="hljs-tag"><span class="hljs-tag"></<span class="hljs-title">relativelayout</span>></span></span></code></pre>7.nextactivity.java<pre class="prettyprint"><code class=" hljs java"><span class="hljs-keyword"><span class="hljs-keyword"></span> public</span> <span class="hljs-class"><span class="hljs-class"> <span class="hljs-keyword">class</span> <span class="hljs-title">nextactivity</span> <span class="hljs-keyword">extends</span> <span class="hljs-title">Activity</span>{</span></span> <span class="hljs-keyword"><span class="hljs-keyword">Private</span></span>Button button;<span class="hljs-annotation"><span class="hljs-annotation">@Override</span></span> <span class="hljs-keyword"><span class="hljs-keyword">protected</span></span> <span class="hljs-keyword"><span class="hljs-keyword">void</span></span> <span class="hljs-title"><span class="hljs-title">onCreate</span></span>(Bundle Savedinstancestate) {<span class="hljs-keyword"><span class="hljs-keyword">Super</span></span>. onCreate (savedinstancestate); Setcontentview (r.layout.activity_next); Initview (); }<span class="hljs-keyword"><span class="hljs-keyword">Private</span></span> <span class="hljs-keyword"><span class="hljs-keyword">void</span></span> <span class="hljs-title"><span class="hljs-title">Initview</span></span>() {button = (button) Findviewbyid (r.id.button); Button.setonclicklistener (<span class="hljs-keyword"><span class="hljs-keyword">New</span></span>Onclicklistener () {<span class="hljs-annotation"><span class="hljs-annotation">@Override</span></span> <span class="hljs-keyword"><span class="hljs-keyword"></span> public</span> <span class="hljs-keyword"><span class="hljs-keyword">void</span></span> <span class="hljs-title"><span class="hljs-title">OnClick</span></span>(View V) {toast.maketext (nextactivity).<span class="hljs-keyword"><span class="hljs-keyword"></span> this</span>, Getrandcolorcode (), toast.length_short). show ();<span class="hljs-comment"><span class="hljs-comment">//send Message</span></span>Eventbus.getdefault (). Post (<span class="hljs-keyword"><span class="hljs-keyword">New</span></span>Changetextcolorevent (getrandcolorcode (), Nextactivity.<span class="hljs-keyword"><span class="hljs-keyword"></span> this</span>)); } }); }<span class="hljs-javadoc"><span class="hljs-javadoc">/** * Gets the hexadecimal color code. for example, "#6E36B4" *<span class="hljs-javadoctag"> @return</span> String</span> */</span> <span class="hljs-keyword"><span class="hljs-keyword"></span> public</span> <span class="hljs-keyword"><span class="hljs-keyword">Static</span></span>String<span class="hljs-title"><span class="hljs-title">Getrandcolorcode</span></span>() {String r, g, b; Random random =<span class="hljs-keyword"><span class="hljs-keyword">New</span></span>Random (); r = integer.tohexstring (random.nextint (<span class="hljs-number"><span class="hljs-number"></span> the</span>). toUpperCase (); g = integer.tohexstring (random.nextint (<span class="hljs-number"><span class="hljs-number"></span> the</span>). toUpperCase (); b = integer.tohexstring (random.nextint (<span class="hljs-number"><span class="hljs-number"></span> the</span>). toUpperCase (); r = R.length () = =<span class="hljs-number"><span class="hljs-number">1</span></span>?<span class="hljs-string"><span class="hljs-string">"0"</span></span>+ r:r; G = g.length () = =<span class="hljs-number"><span class="hljs-number">1</span></span>?<span class="hljs-string"><span class="hljs-string">"0"</span></span>+ g:g; b = b.length () = =<span class="hljs-number"><span class="hljs-number">1</span></span>?<span class="hljs-string"><span class="hljs-string">"0"</span></span>+ b:b;<span class="hljs-keyword"><span class="hljs-keyword">return</span></span> <span class="hljs-string"><span class="hljs-string">"#"</span></span>+r + G + b; }}</code></pre>8. Receive function<p><p>Eventbus In addition to the use of the Oneventmainthread () function to receive, in fact Eventbus there are several different functions, they are:</p></p><p><p>1, onEvent<br>2, Oneventmainthread<br>3, Oneventbackgroundthread<br>4, Oneventasync</p></p><p><p>All four subscription functions start with onevent, they have a slightly different function and introduce two concepts before they are introduced:<br>Informs the Observer that the event occurs when the Eventbus.post function is implemented, which is called the release of the event, and the Observer is told that the event occurred called the receiving of the event, which is implemented by the following subscription Function.</p></p> <ol> <ol> <li>OnEvent: If you use OnEvent as the subscription function, the event is published on which thread, and onEvent runs in that thread, which means that the publish event and receive event threads are on the same thread. With this approach, time-consuming operations cannot be performed in the OnEvent method, which can lead to delays in event distribution if the execution of time-consuming operations is easy.<br><br></li> <li>Oneventmainthread: If you use Oneventmainthread as the subscription function, the Oneventmainthread executes in the UI thread regardless of which thread the event was published in, and the receive event runs in the UI Thread. This is very useful in Android because you can only follow the new UI in the UI thread in android, so it is not possible to perform time-consuming operations in the Onevnetmainthread Method.<br><br></li> <li>Oneventbackground: If you use Oneventbackgrond as the subscription function, if the event is published in the UI thread, then Oneventbackground will run in the child thread if the event is originally published in the Child Thread. , then the Oneventbackground function executes directly in that child Thread.<br><br></li> <li>Oneventasync: using this function as a subscription function, the new child thread will be created to execute oneventasync regardless of which thread the event is published On.</li> </ol> </ol><br><p><p>Note:</p></p><p><p>1.eventbus-v3.0.1.jar Download.</p></p><p><p>2. This article is an example source code DOWNLOAD.</p></p><p><p><br></p></p><p><p>Reference article:</p></p><p><p>1.BroadcastReceiver, the advantages and disadvantages of eventbus.</p></p><p><p>2.EventBus use detailed (a)--preliminary use Eventbus.</p></p> <p><p>"Android" Eventbus Two-minute Introductory instance</p></p></span>
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