查看: 2242|回复: 0

[Android教程] Android EventLine框架制作过程 二 Activity上主线程与子线程之间的通信

发表于 2018-2-20 08:00:03

github项目代码地址,欢迎start

https://github.com/979451341/EventLine

接着上一篇继续写,这次我们要在接收函数上使用元注解来区别接收函数需要在哪个线程执行

  1. @Target(ElementType.METHOD)
  2. @Retention(RetentionPolicy.RUNTIME)
  3. public @interface Process {
  4. int value() default 0;
  5. }
复制代码

然后在EventLine里面添加两个常量

  1. public final static int MainThread = 0;
  2. public final static int SubThread = 1;
复制代码


使用元注解

  1. @Process(EventLine.SubThread)
  2. public void receive(DataBean dataBean) throws InterruptedException {
  3. Log.v("zzw","TwoActivity 接收到了"+dataBean.data);
  4. Thread.sleep(3000);
  5. Log.v("zzw","TwoActivity 接收结束");
  6. }
复制代码

实现

我们需要得到activity的receive函数Process这个元注解的值来判断在哪个线程执行

  1. final Method declaredMethod = cls.getDeclaredMethod("receive", ojb.getClass());
  2. Annotation[] annotations = declaredMethod.getAnnotations();
  3. for(Annotation annotation : annotations){
  4. if(annotation.annotationType() == Process.class){
  5. Process process = (Process)annotation;
  6. value = process.value();
  7. }
  8. }
复制代码

得到元注解的值后根据情况来执行

  1. if(value == MainThread)
  2. declaredMethod.invoke(activity, (Object) ojb);
  3. else if(value == SubThread){
  4. new Thread(new Runnable() {
  5. @Override
  6. public void run() {
  7. try {
  8. declaredMethod.invoke(activity, (Object) ojb);
  9. } catch (IllegalAccessException e) {
  10. e.printStackTrace();
  11. } catch (InvocationTargetException e) {
  12. e.printStackTrace();
  13. }
  14. }
  15. }).start();
  16. }
复制代码

在主线程发送信息

  1. DataBean dataBean = new DataBean();
  2. dataBean.data = "来自ThreeActivity的消息";
  3. EventLine.getInstance().postData(dataBean);
复制代码

效果良好

  1. 01-25 16:57:27.562 31938-32011/com.example.zth.eventline V/zzw: MainActivity 接收到了来自ThreeActivity的消息
  2. 01-25 16:57:27.574 31938-32024/com.example.zth.eventline V/zzw: TwoActivity 接收到了来自ThreeActivity的消息
  3. 01-25 16:57:30.575 31938-32024/com.example.zth.eventline V/zzw: TwoActivity 接收结束
复制代码

现在我们把发送信息的环节放在子线程

  1. new Thread(new Runnable() {
  2. @Override
  3. public void run() {
  4. DataBean dataBean = new DataBean();
  5. dataBean.data = "来自ThreeActivity的消息";
  6. EventLine.getInstance().postData(dataBean);
  7. }
  8. }).start();
复制代码

效果依旧良好

  1. 01-25 16:57:27.562 31938-32011/com.example.zth.eventline V/zzw: MainActivity 接收到了来自ThreeActivity的消息
  2. 01-25 16:57:27.574 31938-32024/com.example.zth.eventline V/zzw: TwoActivity 接收到了来自ThreeActivity的消息
  3. 01-25 16:57:30.575 31938-32024/com.example.zth.eventline V/zzw: TwoActivity 接收结束
复制代码

但是还是对EventLine做出一些改动,在元注解说要在主线程执行接收函数,

  1. if(value == MainThread){
  2. activity.runOnUiThread(new Runnable() {
  3. @Override
  4. public void run() {
  5. try {
  6. declaredMethod.invoke(activity, (Object) ojb);
  7. } catch (IllegalAccessException e) {
  8. e.printStackTrace();
  9. } catch (InvocationTargetException e) {
  10. e.printStackTrace();
  11. }
  12. }
  13. });
  14. }
  15. else if(value == SubThread){
  16. new Thread(new Runnable() {
  17. @Override
  18. public void run() {
  19. try {
  20. declaredMethod.invoke(activity, (Object) ojb);
  21. } catch (IllegalAccessException e) {
  22. e.printStackTrace();
  23. } catch (InvocationTargetException e) {
  24. e.printStackTrace();
  25. }
  26. }
  27. }).start();
  28. }
复制代码


贴出EventLine完整代码

  1. public class EventLine<T> {
  2. public static EventLine eventLine;
  3. public final static int MainThread = 0;
  4. public final static int SubThread = 1;
  5. private EventLine(){
  6. }
  7. public static EventLine getInstance(){
  8. if(eventLine == null){
  9. synchronized (EventLine.class){
  10. if(eventLine == null)
  11. eventLine = new EventLine();
  12. }
  13. }
  14. return eventLine;
  15. }
  16. private ArrayList<Activity> activities = new ArrayList<Activity>();
  17. public void addActivity(Activity activity){
  18. activities.add(activity);
  19. }
  20. public void removeActivity(Activity activity){
  21. activities.remove(activity);
  22. }
  23. public void finishAll(){
  24. for(Activity activity : activities){
  25. activity.finish();
  26. }
  27. }
  28. public void postData(final T ojb){
  29. for(final Activity activity : activities){
  30. int value = 0;
  31. Class<? extends Activity> cls = activity.getClass();
  32. try {
  33. final Method declaredMethod = cls.getDeclaredMethod("receive", ojb.getClass());
  34. Annotation[] annotations = declaredMethod.getAnnotations();
  35. for(Annotation annotation : annotations){
  36. if(annotation.annotationType() == Process.class){
  37. Process process = (Process)annotation;
  38. value = process.value();
  39. }
  40. }
  41. if(value == MainThread){
  42. activity.runOnUiThread(new Runnable() {
  43. @Override
  44. public void run() {
  45. try {
  46. declaredMethod.invoke(activity, (Object) ojb);
  47. } catch (IllegalAccessException e) {
  48. e.printStackTrace();
  49. } catch (InvocationTargetException e) {
  50. e.printStackTrace();
  51. }
  52. }
  53. });
  54. }
  55. else if(value == SubThread){
  56. new Thread(new Runnable() {
  57. @Override
  58. public void run() {
  59. try {
  60. declaredMethod.invoke(activity, (Object) ojb);
  61. } catch (IllegalAccessException e) {
  62. e.printStackTrace();
  63. } catch (InvocationTargetException e) {
  64. e.printStackTrace();
  65. }
  66. }
  67. }).start();
  68. }
  69. } catch (NoSuchMethodException e) {
  70. e.printStackTrace();
  71. }
  72. }
  73. }
  74. }
复制代码


结束了,下一次写一下Fragment与Fragment、activity之间的消息传递





回复

使用道具 举报