切换风格

默认Lavender Sky Flowers Wizard Snow Beige California City Dragon Black London Sunset glow Pink Cloud

31

主题

239

积分

0

精华

用户组 

易积分
815
热心
0
好评
1
随缘_多线线程操作开源代码[复制链接]
发表于 2022-9-29 08:01:30 | 显示全部楼层 |阅读模式
作者:随缘QQ:874334395
QQ群:476412098

  1. package com.e4a.runtime.components.impl.android.随缘_多线程类库;

  2. import android.os.Handler;
  3. import android.os.Looper;
  4. import com.e4a.runtime.components.ComponentContainer;
  5. import com.e4a.runtime.components.impl.ComponentImpl;
  6. import com.e4a.runtime.events.EventDispatcher;
  7. import com.e4a.runtime.variants.Variant;
  8. import java.lang.reflect.Method;
  9. import java.util.ArrayList;
  10. import java.util.HashMap;
  11. import java.util.List;
  12. import java.util.Map;

  13. public class 随缘_多线程Impl extends ComponentImpl implements 随缘_多线程 {
  14.     public final static Map<ComponentContainer, List<Method>> COMPONENT_LIST_MAP = new HashMap<>();
  15.     private ComponentContainer container;
  16.     private Handler handler;

  17.     public 随缘_多线程Impl(ComponentContainer container) {
  18.         super(container);
  19.         this.container = container;
  20.         List<Method> methodList = COMPONENT_LIST_MAP.get(container);
  21.         if (methodList == null) {
  22.             methodList = new ArrayList<>();
  23.             for (Method method : container.getClass().getMethods()) {
  24.                 if (method.getReturnType().equals(Void.TYPE)) {
  25.                     methodList.add(method);
  26.                 }
  27.             }
  28.             COMPONENT_LIST_MAP.put(container, methodList);
  29.         }
  30.         handler = new Handler(Looper.getMainLooper());
  31.     }

  32.     @Override
  33.     public long 启动线程(String 欲被调用的窗口过程名, Variant[] 欲投递的参数) {
  34.         final long id = System.currentTimeMillis();
  35.         new Thread() {
  36.             @Override
  37.             public void run() {
  38.                 try {
  39.                     getMethod(欲被调用的窗口过程名).invoke(container, getPostingParameters(欲投递的参数));
  40.                 } catch (Throwable e) {
  41.                     e.printStackTrace();
  42.                     handler.post(() -> 发生错误(id, e.getMessage()));
  43.                 }
  44.             }
  45.         }.start();
  46.         return id;
  47.     }

  48.     @Override
  49.     public void post主线程(String 欲被调用的窗口过程名, Variant[] 欲投递的参数) {
  50.         handler.post(() -> {
  51.             try {
  52.                 getMethod(欲被调用的窗口过程名).invoke(container, getPostingParameters(欲投递的参数));
  53.             } catch (Throwable e) {
  54.                 e.printStackTrace();
  55.             }
  56.         });
  57.     }

  58.     private Method getMethod(String methodName) throws NullPointerException {
  59.         List<Method> methodList = COMPONENT_LIST_MAP.get(container);
  60.         for (Method method : methodList) {
  61.             if (method.getName().equals(methodName.trim())) {
  62.                 return method;
  63.             }
  64.         }
  65.         throw new NullPointerException(String.format("在窗口下找不到_方法名:'%s' ", methodName));
  66.     }

  67.     private Object[] getPostingParameters(Variant[] variants) {
  68.         if (variants == null || variants.length == 0) {
  69.             return new Object[]{};
  70.         } else {
  71.             Object[] objects = new Object[variants.length];
  72.             for (int i = 0; i < variants.length; i++) {
  73.                 Variant variant = variants[i];
  74.                 switch (variant.getType()) {
  75.                     case 1:
  76.                         objects[i] = variant.getBoolean();
  77.                         break;
  78.                     case 2:
  79.                         objects[i] = variant.getByte();
  80.                         break;
  81.                     case 3:
  82.                         objects[i] = variant.getShort();
  83.                         break;
  84.                     case 4:
  85.                         objects[i] = variant.getInteger();
  86.                         break;
  87.                     case 5:
  88.                         objects[i] = variant.getLong();
  89.                         break;
  90.                     case 6:
  91.                         objects[i] = variant.getSingle();
  92.                         break;
  93.                     case 7:
  94.                         objects[i] = variant.getDouble();
  95.                         break;
  96.                     case 8:
  97.                         objects[i] = variant.getString();
  98.                         break;
  99.                     case 9:
  100.                         objects[i] = variant.getObject();
  101.                         break;
  102.                     case 10:
  103.                         objects[i] = variant.getArray();
  104.                         break;
  105.                     case 11:
  106.                         objects[i] = variant.getDate();
  107.                         break;
  108.                     default:
  109.                         objects[i] = null;
  110.                         break;

  111.                 }
  112.             }
  113.             return objects;
  114.         }

  115.     }

  116.     @Override
  117.     public void 发生错误(long 线程ID, String err) {
  118.         EventDispatcher.dispatchEvent(this, "发生错误", 线程ID, err);
  119.     }
  120. }
复制代码


易如意中文编程学习交流论坛有你更精彩~
回复

使用道具 举报

5

主题

511

积分

0

精华

用户组 

易积分
3045
热心
0
好评
0

活跃会员

发表于 2022-9-29 08:26:10 | 显示全部楼层
MEILUANYOOONG
易如意中文编程学习交流论坛有你更精彩~
回复

使用道具 举报

31

主题

239

积分

0

精华

用户组 

易积分
815
热心
0
好评
1
发表于 2022-9-29 14:37:10 来自手机 | 显示全部楼层
czwy888 发表于 2022-9-29 08:26
MEILUANYOOONG

关键你这货色能看懂不。
易如意中文编程学习交流论坛有你更精彩~
回复

使用道具 举报

12

主题

992

积分

0

精华

用户组 

易积分
6405
热心
0
好评
0

活跃会员

发表于 2022-9-30 11:05:28 | 显示全部楼层
要能看懂这个个.我也就不学E4A啦
易如意中文编程学习交流论坛有你更精彩~
回复

使用道具 举报

26

主题

398

积分

0

精华

用户组 

易积分
59
热心
0
好评
1
发表于 2022-10-2 06:17:10 | 显示全部楼层
没有例程,没用
易如意中文编程学习交流论坛有你更精彩~
回复

使用道具 举报

3

主题

35

积分

0

精华

用户组 

易积分
16
热心
0
好评
0
发表于 2022-10-2 11:26:30 | 显示全部楼层
易如意中文编程学习交流论坛有你更精彩~
回复

使用道具 举报

0

主题

16

积分

0

精华

用户组 

易积分
183
热心
0
好评
0
发表于 2022-10-18 08:33:43 | 显示全部楼层
谢谢分享。
易如意中文编程学习交流论坛有你更精彩~
回复

使用道具 举报

QQ|sitemap|免责声明|RGB颜色对照表|手机版|小黑屋| 易如意 - E4A中文编程学习交流论坛

GMT+8, 2024-5-17 18:14 , Processed in 0.054198 second(s), 32 queries .

Powered by Discuz! X3.4

© 2001-2018 eruyi.cn

返回顶部