This is the seventh day of my participation in the August More text Challenge. For details, see:August is more challenging

👉 About the author

As we all know, life is a long process of overcoming difficulties, reflecting and moving forward. In this process, there will be a lot of questions and thinking about life, so I decided to share my own thinking, experience and story, in order to find resonance!! Focus on Android/Unity and various game development skills, as well as various resource sharing (website, tools, materials, source code, games, etc.)

👉 Soon to learn

We’ve already learned a lot about EventBus in the last two sections, so today we’re going to take you to one of these frameworks.

👉 background

🙈 xiao Kong (bad smile) : Xiao Zhi, how about yesterday and the day before yesterday?

🙎 Xiaozhi (😝) : Why not? I certainly haven’t learned it.

🙈 Xiaokong (😎) : Ok, today we continue, directly write a similar framework.

🙎 Xiaozhi (😥) : Wait, I said I didn’t learn. Ah?

👉 Practice Process

Basic reflection calls

The first is the event bus utility class, we need to manage a lot, so it is necessary to generalize

public static EventCar eventCar;
private EventCar() {
 
}
Copy the code

Add, remove, and other similar registration methods, because the Activity/Fragment type, so use obj type is appropriate

private ArrayList<Object> objects = new ArrayList<Object>();
 
public void add(Object object){
    objects.add(object);
}
 
public void remove(Object object){
    objects.remove(object);
 
}
Copy the code

At this point the basic registration unregistration comes online, calling the two methods we just wrote above.

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    initView();
    EventCar.getInstance().add(this);
}
 
@Override
protected void onDestroy() {
    EventCar.getInstance().remove(this);
    super.onDestroy();
}
Copy the code

Here’s the little key. At this point, the reflection call to the function is going to go up, because it’s not just the Activity, so we need to separate it out

public void post(T obj) { for (Object object : If (object instanceof activity) {Activity Activity = (activity) object; Class<? extends Activity> cls =activity.getClass(); try { Method declaredMethod = cls.getDeclaredMethod("receive", obj.getClass()); //"" < declaredMethod.invoke(activity, (Object) obj); } catch (InvocationTargetException e) { e.printStackTrace(); } catch (IllegalAccessException e) { e.printStackTrace(); } catch (NoSuchMethodException e) { e.printStackTrace(); }}}}Copy the code

All right, let’s do a little experiment. See if it works

Public void receive(MessageEvent MSG) {log.e ("TAG"," message received "+ MSG.getName()); } @Override protected void onDestroy() { EventCar.getInstance().remove(this); super.onDestroy(); } @Override public void onClick(View v) { switch (v.getId()) { default: break; Case R.I.D.C eshi: eventCar.getInstance ().post(new MessageEvent(" ")); break; }}Copy the code

Run, and the result appears

Well, look at that. Let’s keep going.

Specify the receiving thread

@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
public @interface Process {
 
    int value() default 0;
 
}
Copy the code

Let’s call it and see

@process (eventcar.subthread) public void receive(MessageEvent MSG) { Log.e("TAG"," message received "+ MSG. GetName ()); }Copy the code

Go back to EventCar to complete the logic

final Method declaredMethod = cls.getDeclaredMethod("receive", obj.getClass()); / / "" the name of the method of the [] of an Annotation annotations. = declaredMethod getAnnotations (); for(Annotation annotation : }}}}}}}}}}}}}}}}}}}}} Traverse to obtain the corresponding Process markup annotations if (annotation. AnnotationType () = = Process. The class) {Process Process = (Process) the annotation; value = process.value(); }} if(value == MainThread){activity.runonuithRead (new Runnable() {// in the UI thread @override public void run() {try { declaredMethod.invoke(activity, (Object) obj); } catch (IllegalAccessException e) { e.printStackTrace(); } catch (InvocationTargetException e) { e.printStackTrace(); }}}); }else if(value == SubThread){new Thread(new Runnable() @override public void run() {try { declaredMethod.invoke(activity, (Object) obj); } catch (IllegalAccessException e) { e.printStackTrace(); } catch (InvocationTargetException e) { e.printStackTrace(); } } }).start(); }Copy the code

Go to the test, gee, it’s still ok;

Good, met the activity of the fragments/activity/service all kinds of cross communication, this is also the reason why we use generics and obj type above

Final fixes to improve EventCar:

Public void post(final T obj) {for (Object Object: objects) {int value = 0; if (object instanceof Activity) { final Activity activity = (Activity) object; Class<? extends Activity> cls = activity.getClass(); try { final Method declaredMethod = cls.getDeclaredMethod("receive", obj.getClass()); / / "" the name of the method of the [] of an Annotation annotations. = declaredMethod getAnnotations (); for (Annotation annotation : }}}}}}}}}}}}}}}}}}}}} Traverse to obtain the corresponding Process markup annotations if (annotation. AnnotationType () = = Process. The class) {Process Process = (Process) the annotation; value = process.value(); }} if (value == MainThread) {activity.runonuithRead (new Runnable() {// in the UI thread @override public void run() {try { declaredMethod.invoke(activity, (Object) obj); } catch (IllegalAccessException e) { e.printStackTrace(); } catch (InvocationTargetException e) { e.printStackTrace(); }}}); } else if (value == SubThread) {new Thread(new Runnable() @override public void run() {try { declaredMethod.invoke(activity, (Object) obj); } catch (IllegalAccessException e) { e.printStackTrace(); } catch (InvocationTargetException e) { e.printStackTrace(); } } }).start(); } } catch (NoSuchMethodException e) { e.printStackTrace(); } } else if (object instanceof Fragment) { final Fragment fragment = (Fragment) object; Class<? extends Fragment> cls = fragment.getClass(); try { final Method declaredMethod = cls.getDeclaredMethod("receive", obj.getClass()); Annotation[] annotations = declaredMethod.getAnnotations(); for (Annotation annotation : annotations) { if (annotation.annotationType() == Process.class) { Process process = (Process) annotation; value = process.value(); } } if (value == MainThread) { fragment.getActivity().runOnUiThread(new Runnable() { @Override public void run() { try {  declaredMethod.invoke(fragment, (Object) obj); } catch (IllegalAccessException e) { e.printStackTrace(); } catch (InvocationTargetException e) { e.printStackTrace(); }}}); } else if (value == SubThread) { new Thread(new Runnable() { @Override public void run() { try { declaredMethod.invoke(fragment, (Object) obj); } catch (IllegalAccessException e) { e.printStackTrace(); } catch (InvocationTargetException e) { e.printStackTrace(); } } }).start(); } } catch (NoSuchMethodException e) { e.printStackTrace(); } } else if (object instanceof Service) { final Service service = (Service) object; Class<? extends Service> cls = service.getClass(); try { final Method declaredMethod = cls.getDeclaredMethod("receive", obj.getClass()); Annotation[] annotations = declaredMethod.getAnnotations(); for (Annotation annotation : annotations) { if (annotation.annotationType() == Process.class) { Process process = (Process) annotation; value = process.value(); } } if (value == MainThread) { try { declaredMethod.invoke(service, (Object) obj); } catch (IllegalAccessException e) { e.printStackTrace(); } catch (InvocationTargetException e) { e.printStackTrace(); } } else if (value == SubThread) { new Thread(new Runnable() { @Override public void run() { try { declaredMethod.invoke(service, (Object) obj); } catch (IllegalAccessException e) { e.printStackTrace(); } catch (InvocationTargetException e) { e.printStackTrace(); } } }).start(); } } catch (NoSuchMethodException e) { e.printStackTrace(); }}}}Copy the code

👉 other

📢 by xiao Kong and xiao Kong in Xiao Zhi 📢. 📢 Welcome to like 👍 save 🌟 leave 📝