package com.lzy.evbusdemo;
public class MyEvent {
private String msg;
public MyEvent(String msg) {
this.msg = msg;
}
public String getMsg() {
return msg;
}
}
其他两个主文件
package com.lzy.evbusdemo;
import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.util.Log;
import android.view.View;
import android.widget.TextView;
public class MainActivity extends Activity {
private TextView tvShowTextView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
tvShowTextView = (TextView) findViewById(R.id.tvShow);
EventBus.getDefault().register(this);// 订阅
}
public void jumpTo(View view) {
startActivity(new Intent(this, OtherActivity.class));
}
@Override
protected void onDestroy() {
super.onDestroy();
EventBus.getDefault().unregister(this);
}
/**
* 接收
* */
@Subscribe
public void onEvent(MyEvent event) {
Log.i("Main", "Main - onEvent: " + event.getMsg());
}
@Subscribe
public void onEventAsync(MyEvent event) {
Log.i("Main", "Main - onEventAsync: " + event.getMsg());
}
@Subscribe
public void onEventBackgroundThread(MyEvent event) {
Log.i("Main", "Main - onEventBackgroundThread: " + event.getMsg());
}
@Subscribe
public void onEventMainThread(MyEvent event) {
Log.i("Main", "Main - onEventMainThread: " + event.getMsg());
tvShowTextView.setText(event.getMsg());
}
}
package com.lzy.evbusdemo;
import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup.LayoutParams;
import android.widget.TextView;
public class OtherActivity extends Activity {
private TextView textView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_other);
EventBus.getDefault().register(this);
textView = new TextView(this);
textView.setText(getResources().getString(R.string.Text_Other_Activity));
addContentView(textView, new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
textView.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
EventBus.getDefault().post(new MyEvent("LD"));// 发布
}
});
}
@Override
protected void onDestroy() {
super.onDestroy();
EventBus.getDefault().unregister(this);
}
/**
* 接收
* */
@Subscribe
public void onEvent(MyEvent event) {
Log.i("Other", "Other - onEvent: " + event.getMsg());
}
@Subscribe
public void onEventAsync(MyEvent event) {
Log.i("Other", "Other - onEventAsync: " + event.getMsg());
}
@Subscribe
public void onEventBackgroundThread(MyEvent event) {
Log.i("Other", "Other - onEventBackgroundThread: " + event.getMsg());
}
@Subscribe
public void onEventMainThread(MyEvent event) {
Log.i("Other", "Other - onEventMainThread: " + event.getMsg());
textView.setText(event.getMsg());
}
}
通过以上我们就实现了订阅与分发,只要是订阅者,在发送的时候就都能收到了。不过这几个方法我们应该使用哪个呢,还有他们是怎么调用的呢?上述代码调试信息如下
package com.lzy.evbusdemo;
import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.util.Log;
import android.view.View;
import android.widget.TextView;
public class MainActivity extends Activity {
private TextView tvShowTextView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
tvShowTextView = (TextView) findViewById(R.id.tvShow);
EventBus.getDefault().register(this);// 订阅
}
public void jumpTo(View view) {
startActivity(new Intent(this, OtherActivity.class));
}
@Override
protected void onDestroy() {
super.onDestroy();
EventBus.getDefault().unregister(this);
}
/**
* 接收
* */
@Subscribe
public void onEvent(MyEvent event) {
Log.i("Main", "Main - onEvent: " + event.getMsg());
}
@Subscribe
public void onEventAsync(MyEvent1 event) {
Log.i("Main", "Main - onEventAsync: " + event.getMsg());
}
@Subscribe
public void onEventBackgroundThread(MyEvent2 event) {
Log.i("Main", "Main - onEventBackgroundThread: " + event.getMsg());
}
@Subscribe
public void onEventMainThread(MyEvent3 event) {
Log.i("Main", "Main - onEventMainThread: " + event.getMsg());
tvShowTextView.setText(event.getMsg());
}
}
package com.lzy.evbusdemo;
import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup.LayoutParams;
import android.widget.TextView;
public class OtherActivity extends Activity {
private TextView textView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_other);
EventBus.getDefault().register(this);
textView = new TextView(this);
textView.setText(getResources().getString(R.string.Text_Other_Activity));
addContentView(textView, new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
textView.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
EventBus.getDefault().post(new MyEvent("LD"));// 发布
EventBus.getDefault().post(new MyEvent1("DCM"));
EventBus.getDefault().post(new MyEvent2("HY"));
EventBus.getDefault().post(new MyEvent3("JSM"));
}
});
}
@Override
protected void onDestroy() {
super.onDestroy();
EventBus.getDefault().unregister(this);
}
/**
* 接收
* */
@Subscribe
public void onEvent(MyEvent event) {
Log.i("Other", "Other - onEvent: " + event.getMsg());
}
@Subscribe
public void onEventAsync(MyEvent3 event) {
Log.i("Other", "Other - onEventAsync: " + event.getMsg());
}
@Subscribe
public void onEventBackgroundThread(MyEvent1 event) {
Log.i("Other", "Other - onEventBackgroundThread: " + event.getMsg());
}
@Subscribe
public void onEventMainThread(MyEvent2 event) {
Log.i("Other", "Other - onEventMainThread: " + event.getMsg());
textView.setText(event.getMsg());
}
}
运行一下:
int size = subscriptions.size();
for (int i = 0; i <= size; i++) {
if (i == size || subscriberMethod.priority > subscriptions.get(i).subscriberMethod.priority) {
subscriptions.add(i, newSubscription);
break;
}
}
List> subscribedEvents = typesBySubscriber.get(subscriber);
if (subscribedEvents == null) {
subscribedEvents = new ArrayList<>();
typesBySubscriber.put(subscriber, subscribedEvents);
}
subscribedEvents.add(eventType);
if (subscriberMethod.sticky) {
if (eventInheritance) {
// Existing sticky events of all subclasses of eventType have to be considered.
// Note: Iterating over all events may be inefficient with lots of sticky events,
// thus data structure should be changed to allow a more efficient lookup
// (e.g. an additional map storing sub classes of super classes: Class -> List).
Set, Object>> entries = stickyEvents.entrySet();
for (Map.Entry, Object> entry : entries) {
Class> candidateEventType = entry.getKey();
if (eventType.isAssignableFrom(candidateEventType)) {
Object stickyEvent = entry.getValue();
checkPostStickyEventToSubscription(newSubscription, stickyEvent);
}
}
} else {
Object stickyEvent = stickyEvents.get(eventType);
checkPostStickyEventToSubscription(newSubscription, stickyEvent);
}
}
private void findUsingReflectionInSingleClass(FindState findState) {
Method[] methods;
try {
// This is faster than getMethods, especially when subscribers are fat classes like Activities
methods = findState.clazz.getDeclaredMethods();
} catch (Throwable th) {
// Workaround for java.lang.NoClassDefFoundError, see https://github.com/greenrobot/EventBus/issues/149
methods = findState.clazz.getMethods();
findState.skipSuperClasses = true;
}
for (Method method : methods) {
int modifiers = method.getModifiers();
if ((modifiers & Modifier.PUBLIC) != 0 && (modifiers & MODIFIERS_IGNORE) == 0) {
Class>[] parameterTypes = method.getParameterTypes();
if (parameterTypes.length == 1) {
Subscribe subscribeAnnotation = method.getAnnotation(Subscribe.class);
if (subscribeAnnotation != null) {
Class> eventType = parameterTypes[0];
if (findState.checkAdd(method, eventType)) {
ThreadMode threadMode = subscribeAnnotation.threadMode();
findState.subscriberMethods.add(new SubscriberMethod(method, eventType, threadMode,
subscribeAnnotation.priority(), subscribeAnnotation.sticky()));
}
}
} else if (strictMethodVerification && method.isAnnotationPresent(Subscribe.class)) {
String methodName = method.getDeclaringClass().getName() + "." + method.getName();
throw new EventBusException("@Subscribe method " + methodName +
"must have exactly 1 parameter but has " + parameterTypes.length);
}
} else if (strictMethodVerification && method.isAnnotationPresent(Subscribe.class)) {
String methodName = method.getDeclaringClass().getName() + "." + method.getName();
throw new EventBusException(methodName +
" is a illegal @Subscribe method: must be public, non-static, and non-abstract");
}
}
}
筛选:
private SubscriberInfo getSubscriberInfo(FindState findState) {
if (findState.subscriberInfo != null && findState.subscriberInfo.getSuperSubscriberInfo() != null) {
SubscriberInfo superclassInfo = findState.subscriberInfo.getSuperSubscriberInfo();
if (findState.clazz == superclassInfo.getSubscriberClass()) {
return superclassInfo;
}
}
if (subscriberInfoIndexes != null) {
for (SubscriberInfoIndex index : subscriberInfoIndexes) {
SubscriberInfo info = index.getSubscriberInfo(findState.clazz);
if (info != null) {
return info;
}
}
}
return null;
}
PostingThreadState postingState = currentPostingThreadState.get();
List
然后就一直在寻找可以接收的接受者:
private boolean postSingleEventForEventType(Object event, PostingThreadState postingState, Class> eventClass) {
CopyOnWriteArrayList subscriptions;
synchronized (this) {
subscriptions = subscriptionsByEventType.get(eventClass);
}
if (subscriptions != null && !subscriptions.isEmpty()) {
for (Subscription subscription : subscriptions) {
postingState.event = event;
postingState.subscription = subscription;
boolean aborted = false;
try {
postToSubscription(subscription, event, postingState.isMainThread);
aborted = postingState.canceled;
} finally {
postingState.event = null;
postingState.subscription = null;
postingState.canceled = false;
}
if (aborted) {
break;
}
}
return true;
}
return false;
}
将对应事件所有的找到后
【欢迎上码】
【微信公众号搜索 h2o2s2】