Android 手动撸出一个事件总线框架 二 Activity上主线程与子线程之间的通信
程序员文章站
2022-04-14 13:29:40
github项目代码地址,欢迎start https://github.com/979451341/EventLine 接着上一篇继续写,这次我们要在接收函数上使用元注解来区别接收函数需要在哪个线程执行 然后在EventLine里面添加两个常量 使用元注解 实现我们需要得到activity的rece ......
github项目代码地址,欢迎start
https://github.com/979451341/EventLine
接着上一篇继续写,这次我们要在接收函数上使用元注解来区别接收函数需要在哪个线程执行
@Target(ElementType.METHOD) @Retention(RetentionPolicy.RUNTIME) public @interface Process { int value() default 0; }
然后在EventLine里面添加两个常量
public final static int MainThread = 0; public final static int SubThread = 1;
使用元注解
@Process(EventLine.SubThread) public void receive(DataBean dataBean) throws InterruptedException { Log.v("zzw","TwoActivity 接收到了"+dataBean.data); Thread.sleep(3000); Log.v("zzw","TwoActivity 接收结束"); }
实现
我们需要得到activity的receive函数Process这个元注解的值来判断在哪个线程执行
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) 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(); }
在主线程发送信息
DataBean dataBean = new DataBean(); dataBean.data = "来自ThreeActivity的消息"; EventLine.getInstance().postData(dataBean);
效果良好
01-25 16:57:27.562 31938-32011/com.example.zth.eventline V/zzw: MainActivity 接收到了来自ThreeActivity的消息 01-25 16:57:27.574 31938-32024/com.example.zth.eventline V/zzw: TwoActivity 接收到了来自ThreeActivity的消息 01-25 16:57:30.575 31938-32024/com.example.zth.eventline V/zzw: TwoActivity 接收结束
现在我们把发送信息的环节放在子线程
new Thread(new Runnable() { @Override public void run() { DataBean dataBean = new DataBean(); dataBean.data = "来自ThreeActivity的消息"; EventLine.getInstance().postData(dataBean); } }).start();
效果依旧良好
01-25 16:57:27.562 31938-32011/com.example.zth.eventline V/zzw: MainActivity 接收到了来自ThreeActivity的消息 01-25 16:57:27.574 31938-32024/com.example.zth.eventline V/zzw: TwoActivity 接收到了来自ThreeActivity的消息 01-25 16:57:30.575 31938-32024/com.example.zth.eventline V/zzw: TwoActivity 接收结束
但是还是对EventLine做出一些改动,在元注解说要在主线程执行接收函数,
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(); }
贴出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(); } } } }
结束了,下一次写一下Fragment与Fragment、activity之间的消息传递
下一篇: HierarchyID 数据类型用法