Android manually releases an event bus framework. 2. Communication between the main thread and the sub-thread on the Activity. androidactivity
Github project code address. Welcome to start
Https://github.com/979451341/EventLine
Next, we will continue writing in the previous article. This time, we will use the meta Annotation on the receiving function to differentiate the thread in which the receiving function needs to be executed.
@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" + dataBean. data); Thread. sleep (3000); Log. v ("zzw", "TwoActivity ends ");}
Implementation
We need to get the value of the Meta annotation of the receive function Process of the activity 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(); } }
Run the command as needed after obtaining the meta annotation 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(); }
Send messages in the main thread
DataBean dataBean = new DataBean (); dataBean. data = "messages from ThreeActivity"; EventLine. getInstance (). postData (dataBean );
Good results
01-25 16:57:27. 562 31938-32011/com. example. zth. eventline V/zzw: The MainActivity receives the message from ThreeActivity 01-25 16:57:27. 574 31938-32024/com. example. zth. eventline V/zzw: TwoActivity receives the message from ThreeActivity 01-25 16:57:30. 575 31938-32024/com. example. zth. eventline V/zzw: TwoActivity ends
Now we put the information sending link in the subthread
New Thread (new Runnable () {@ Override public void run () {DataBean dataBean = new DataBean (); dataBean. data = "messages from ThreeActivity"; EventLine. getInstance (). postData (dataBean );}}). start ();
Good results
01-25 16:57:27. 562 31938-32011/com. example. zth. eventline V/zzw: The MainActivity receives the message from ThreeActivity 01-25 16:57:27. 574 31938-32024/com. example. zth. eventline V/zzw: TwoActivity receives the message from ThreeActivity 01-25 16:57:30. 575 31938-32024/com. example. zth. eventline V/zzw: TwoActivity ends
However, some modifications are made to EventLine. In the meta annotation, the receiving function must 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 the complete EventLine code
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 transmission between Fragment, Fragment, and activity.