Next, we will continue to write the previous article. This time, we will use meta annotation on the receive function to distinguish which thread the receive function needs to execute on
Then add two constants to EventLine@Target(ElementType.METHOD) @Retention(RetentionPolicy.RUNTIME) public @interface Process { int value() default 0; }
public final static int MainThread = 0; public final static int SubThread = 1;
Use meta annotation
Realization@Process(EventLine.SubThread) public void receive(DataBean dataBean) throws InterruptedException { Log.v("zzw","TwoActivity Received it"+dataBean.data); Thread.sleep(3000); Log.v("zzw","TwoActivity End of reception"); }
We need to get the value of the meta annotation of activity's receive function Process to determine which thread to execute
Get the value of meta annotation and execute according to the situationfinal Method declaredMethod = cls.getDeclaredMethod("receive", ojb.getClass()); Annotation[] annotations = declaredMethod.getAnnotations(); for(Annotation annotation : annotations){ if(annotation.annotationType() == Process.class){ Process process = (Process)annotation; value = process.value(); } }
Send message on main threadif(value == MainThread) declaredMethod.invoke(activity, (Object) ojb); else if(value == SubThread){ new Thread(new Runnable() { @Override public void run() { try { declaredMethod.invoke(activity, (Object) ojb); } catch (IllegalAccessException e) { e.printStackTrace(); } catch (InvocationTargetException e) { e.printStackTrace(); } } }).start(); }
Good effectDataBean dataBean = new DataBean(); dataBean.data = "Come from ThreeActivity Message for"; EventLine.getInstance().postData(dataBean);
01-25 16:57:27.562 31938-32011/com.example.zth.eventline V/zzw: MainActivity received a message from ThreeActivity 01-25 16:57:27.574 31938-32024/com.example.zth.eventline V/zzw: TwoActivity received a message from ThreeActivity 01-25 16:57:30.575 31938-32024/com.example.zth.eventline V/zzw: TwoActivity receiving end
The effect is still goodnew Thread(new Runnable() { @Override public void run() { DataBean dataBean = new DataBean(); dataBean.data = "Come from ThreeActivity Message for"; EventLine.getInstance().postData(dataBean); } }).start();
01-25 16:57:27.562 31938-32011/com.example.zth.eventline V/zzw: MainActivity received a message from ThreeActivity 01-25 16:57:27.574 31938-32024/com.example.zth.eventline V/zzw: TwoActivity received a message from ThreeActivity 01-25 16:57:30.575 31938-32024/com.example.zth.eventline V/zzw: TwoActivity receiving end
if(value == MainThread){ activity.runOnUiThread(new Runnable() { @Override public void run() { try { declaredMethod.invoke(activity, (Object) ojb); } 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) ojb); } catch (IllegalAccessException e) { e.printStackTrace(); } catch (InvocationTargetException e) { e.printStackTrace(); } } }).start(); }
Paste out the complete code of EventLine
public class EventLine<T> { public static EventLine eventLine; public final static int MainThread = 0; public final static int SubThread = 1; private EventLine(){ } public static EventLine getInstance(){ if(eventLine == null){ synchronized (EventLine.class){ if(eventLine == null) eventLine = new EventLine(); } } return eventLine; } private ArrayList<Activity> activities = new ArrayList<Activity>(); public void addActivity(Activity activity){ activities.add(activity); } public void removeActivity(Activity activity){ activities.remove(activity); } public void finishAll(){ for(Activity activity : activities){ activity.finish(); } } public void postData(final T ojb){ for(final Activity activity : activities){ int value = 0; Class<? extends Activity> cls = activity.getClass(); try { final Method declaredMethod = cls.getDeclaredMethod("receive", ojb.getClass()); Annotation[] annotations = declaredMethod.getAnnotations(); for(Annotation annotation : annotations){ if(annotation.annotationType() == Process.class){ Process process = (Process)annotation; value = process.value(); } } if(value == MainThread){ activity.runOnUiThread(new Runnable() { @Override public void run() { try { declaredMethod.invoke(activity, (Object) ojb); } 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) ojb); } catch (IllegalAccessException e) { e.printStackTrace(); } catch (InvocationTargetException e) { e.printStackTrace(); } } }).start(); } } catch (NoSuchMethodException e) { e.printStackTrace(); } } } }
It's over. Next time, write the message passing between Fragment, Fragment and activity