Android manually publishes an event bus framework II. Communication between main thread and sub thread on Activity

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

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

}
Then add two constants to EventLine

    public final static int MainThread = 0;
    public final static int SubThread = 1;

Use meta annotation
    @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");
    }
Realization

We need to get the value of the meta annotation of activity's receive function Process to determine which thread to execute

                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();

                    }
                }
Get the value of meta annotation and execute according to the situation
                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();

                }
Send message on main thread
                DataBean dataBean = new DataBean();
                dataBean.data = "Come from ThreeActivity Message for";
                EventLine.getInstance().postData(dataBean);
Good effect

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
Now let's put the information sending part in the sub thread
                new Thread(new Runnable() {
                    @Override
                    public void run() {

                        DataBean dataBean = new DataBean();
                        dataBean.data = "Come from ThreeActivity Message for";
                        EventLine.getInstance().postData(dataBean);
                    }
                }).start();
The effect is still good
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
However, some changes have been made to EventLine. In the meta note, it is explained that the receive function should be executed in the main thread,

                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










Keywords: Fragment

Added by ThatGuyBob on Sat, 02 May 2020 17:08:31 +0300