<! --http://blog.csdn.net/yuanyang5917/article/details/45482457-->
Event Bus Framework
A solution that provides a unified subscription for events, which is published to achieve communication between components.
Official definition:
Otto is a event bus designed to decouple different parts of your application while still allowing them to communicate eff iciently.
Principle
Observer mode + Annotations + Reflection
Otto Realization Chapter
Here are a few points to note:
(1) Otto uses annotations to define the role of a subscription/publisher, @Subscribe as a subscriber, @Produce as a publisher, and the method name can be customized.
(2) Otto for performance, code intent is clear, @Subscribe, @Produce method must be defined on a direct action class, but not defined in the base class and inherited.
(3) Subscribers/publishers are required to register and unregister, and Eventbus's publishers are not required by them.
(4) The number of event parameters can only be one, not more
-
Defines event-locationchangedevent as a carrier for communicating data between components
/* * Copyright (C) Square, inc. * * Licensed under the Apache License, Version 2.0 (the "License"); &nb sp;* do not use this file except in compliance with the license. * a copy of the obtain License * * http://www.apache.org/licenses/license-2.0 * * unless required by applicable law or agreed to in Writi Ng, software * distributed under the License is distributed on a "as is" basis, * without warranties OR Conditi ONS of any KIND, either express or implied. * see the License for the specific language governing permissions And&nbs p;* limitations under the License. */package Com.squareup.otto.sample;public class Locationchangedevent { Public final float lat; public final float lon; public locationchangedevent (float lat, float lon) {   ; This.lat = lat; This.lon = lon; } @Override public String toString () {&NBSP;&NBSP;&N Bsp return new StringBuilder ("(")// . Append (LAT)// . Append (",")// . Append (Lon)// . Append (")")// . toString (); }}
-
To avoid waste, Otto needs to implement the singleton itself, relative to Busprovider.getinstance ().
/* * Copyright (C) Square, inc. * * Licensed under the Apache License, Version 2.0 (the "License"); &nb sp;* do not use this file except in compliance with the license. * a copy of the obtain License * * http://www.apache.org/licenses/license-2.0 * * unless required by applicable law or agreed to in Writi Ng, software * distributed under the License is distributed on a "as is" basis, * without warranties OR Conditi ONS of any KIND, either express or implied. * see the License for the specific language governing permissions And&nbs p;* limitations under the License. */package com.squareup.otto.sample;import com.squareup.otto.bus;/** * Maintains a singleton instance for obtaining the bus. Ideally this would is replaced with a more efficient means * such as through injection directly into interested class Es. */public Final class Busprovider { private static final bus bus = new bus (); PUBlic static Bus getinstance () { return bus; } private Busprovider () { //N o instances. }}
-
Defines the subscriber, in the method that accepts the event plus the modifier @subscribe
/* * Copyright (C) Square, inc. * * Licensed under the Apache License, Version 2.0 (the "License"); &nb sp;* do not use this file except in compliance with the license. * a copy of the obtain License * * http://www.apache.org/licenses/license-2.0 * * unless required by applicable law or agreed to in Writi Ng, software * distributed under the License is distributed on a "as is" basis, * without warranties OR Conditi ONS of any KIND, either express or implied. * see the License for the specific language governing permissions And&nbs p;* limitations under the License. */package com.squareup.otto.sample;import android.os.bundle;import Android.support.v4.app.listfragment;import Android.view.view;import Android.widget.arrayadapter;import Android.widget.toast;import Com.squareup.otto.subscribe;import Java.util.arraylist;import java.util.List;/** Maintain a scrollable history of the location events. */public class LocatioNhistoryfragment extends Listfragment { private final list<string> locationevents = new Arraylist<string> (); private arrayadapter<string> adapter; @ override public void Onresume () { super.onresume (); busprovider.getinstance (). Register (this); } @ override public void OnPause () { super.onpause (); busprovider.getinstance (). Unregister (this); } @ override public void onviewcreated (view view, Bundle savedinstancestate) { & nbsp super.onviewcreated (view, savedinstancestate); adapter = new Arrayadapter <String> (getactivity (), android. R.layout.simple_list_item_1, locationevents); setlistadapter (adapter); } @Subscribe public void onlocationchanged (locationchangedevent Event) { locationevents.add (0, event.tostring ()); &NBSP;IF (adapter = null) { Adapter.notifydatasetchanged (); } } /** * method to take, parameters can only be unique * Very convenient * @param event */ & nbsp @Subscribe public void turbolocationchanged (Locationchangedevent event) { toast.maketext (Getactivity (), event.tostring (), Toast.length_long) .show (); &NBSP;}&NBsp; @Subscribe public void onlocationcleared (Locationclearevent event) { locationevents.clear (); if (Adapter! = NULL) { adapter.notifydatasetchanged (); } }}
-
Define the publisher, and post the message anywhere
/* * Copyright (C) Square, inc. * * Licensed under the Apache License, Version 2.0 (the "License"); &nb sp;* do not use this file except in compliance with the license. * a copy of the obtain License * * http://www.apache.org/licenses/license-2.0 * * unless required by applicable law or agreed to in Writi Ng, software * distributed under the License is distributed on a "as is" basis, * without warranties OR Conditi ONS of any KIND, either express or implied. * see the License for the specific language governing permissions And&nbs p;* limitations under the License. */package com.squareup.otto.sample;import android.os.bundle;import Android.support.v4.app.fragmentactivity;import Android.view.view;import Com.squareup.otto.produce;import Java.util.random;import static Android.view.view.onclicklistener;public class Locationactivity extends Fragmentactivity { public static final float Default_lat = 40.440866f; public static final float Default_lon = -79.994085f; private static final float OFFSET = 0.1f; private Static final random random = new random (); private static float lastlatitude = default_lat; private static Flo At lastlongitude = default_lon; @Override protected void onCreate (Bundle savedinstancestate) { Super.oncreate (savedinstancestate); Setcontentview (r.layout.location_history); Findviewbyid (r.id.clear_location) Setonclicklistener (New Onclicklistener () { @Override public void OnClick (View v) { //Tell everyone to clear their location history. busprovider.getinstance (). Post (New Locationclearevent ()); //Post New location event for the default location. lastlatitude = default_lat; lastlongitude = default_lon; Busprovider.getinstance (). Post (Producelocationevent ()); } }); Findviewbyid (r.id.move_location) Setonclicklistener (New Onclicklistener () { @Override public void OnClick (View v) { lastlatitude + = ( Random.nextfloat () * OFFSET * 2)-offset; lastlongitude + = (RANDOM.nextFloat () * OFFSET * 2)-offset; busprovider.getinstance (). Post ( Producelocationevent ()); } }); } @Override protected void Onresume () { super.onresume (); //Register ourselves so that we can provide the I Nitial value. busprovider.getinstance (). Register (this); } @Override protected void OnpauSE () { super.onpause (); //Always unregister when an object no longer should is on the bus. busprovider.getinstance (). Unregister (this); } @Produce Public Locationchangedevent producelocationevent () { //provide an initial value for location based on the last Known position. return new Locationchangedevent (Lastlatitude, Lastlongitude); }}
DOWNLOAD ADDRESS (DEMO):http://download.csdn.net/download/yuanyang5917/8660175
Event Bus Otto (helps Android in-depth decoupling – say goodbye to callbacks)