B2B网络软件

标题: 后台子线程runJsFile和runOnUiThread实时反馈 [打印本页]

作者: YYPOST群发软件    时间: 1 小时前
标题: 后台子线程runJsFile和runOnUiThread实时反馈


后台子线程runJsFile和runOnUiThread实时反馈
后台子线程runJsFile和runOnUiThread实时反馈 B2B网络软件

后台子线程runJsFile和runOnUiThread实时反馈 B2B网络软件

后台子线程runJsFile和runOnUiThread实时反馈 B2B网络软件
https://www.yuque.com/aiwork/nba2pr/emximr9gu1eui3tp
  1. // 主脚本.js —— 被 start.js 用 Import() 执行
  2. // 实用型演示:后台子线程(runJsFile)批量处理任务 + 主线程(runOnUiThread)实时反馈进度到悬浮窗。
  3. // 设计原则:子线程只干活+喊话(reportProgress),绝不自己改界面;界面刷新一律走 runOnUiThread。

  4. // —— 全局 UI 引用(顶层 var = 全局,子线程回调里能访问到)——
  5. var gUI = null, gTv = null, gPb = null, gSub = null;

  6. function buildFeedbackUI() {
  7.     gUI = new floatUI();
  8.     gUI.loadXML(
  9.         '<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" ' +
  10.         '  android:layout_width="match_parent" android:layout_height="wrap_content" ' +
  11.         '  android:orientation="vertical" android:padding="12dp">' +
  12.         '  <TextView android:id="tv" android:layout_width="match_parent" android:layout_height="wrap_content" ' +
  13.         '    android:text="准备启动后台任务..." android:textColor="#222222" android:textSize="16sp"/>' +
  14.         '  <TextView android:id="pb" android:layout_width="match_parent" android:layout_height="wrap_content" ' +
  15.         '    android:text="进度:0%" android:textColor="#1565C0" android:textSize="16sp" android:layout_marginTop="6dp"/>' +
  16.         '  <TextView android:id="sub" android:layout_width="match_parent" android:layout_height="wrap_content" ' +
  17.         '    android:text="成功 0 / 失败 0" android:textColor="#666666" android:textSize="14sp" android:layout_marginTop="4dp"/>' +
  18.         '</LinearLayout>'
  19.     );
  20.     gTv  = gUI.findViewById('tv');
  21.     gPb  = gUI.findViewById('pb');
  22.     gSub = gUI.findViewById('sub');
  23. }

  24. // 子线程调用:把"单项进度"刷到界面(必须用 runOnUiThread 切回 UI 线程改控件)
  25. function reportProgress(done, total, ok, fail, status) {
  26.     var t = new thread();
  27.     t.runOnUiThread(function () {
  28.         if (gTv)  gTv.setText(status + '(' + done + '/' + total + ')');
  29.         if (gPb)  gPb.setText('进度:' + Math.round(done * 100 / total) + '%');
  30.         if (gSub) gSub.setText('成功 ' + ok + ' / 失败 ' + fail);
  31.         printl('[UI] 进度刷新:' + done + '/' + total + ',' + status);
  32.     });
  33. }

  34. // 子线程调用:全部完成后的汇总反馈
  35. function reportDone(total, ok, fail) {
  36.     var t = new thread();
  37.     t.runOnUiThread(function () {
  38.         if (gTv)  gTv.setText('✅ 全部完成:共 ' + total + ' 项');
  39.         if (gSub) gSub.setText('成功 ' + ok + ' / 失败 ' + fail + '(失败项见日志)');
  40.     });
  41.     printl('主线程收到汇总:成功 ' + ok + ' / 失败 ' + fail);
  42. }

  43. function demo_runJsFile() {
  44.     buildFeedbackUI();
  45.     printl('界面已弹出,后台子线程开始批量处理');

  46.     var worker = new thread();
  47.     // 同目录相对文件名;runJsFile 自动按脚本目录解析,不要写 /sdcard/ 绝对路径
  48.     worker.runJsFile('worker_file.js', '后台任务线程');

  49.     // 进度由子线程回调刷新,主线程不必空轮询等它。
  50.     // 但必须保活主线程:AIWROK 主线程一跑完,子线程和定时器全死。
  51.     (function keepAlive() {
  52.         if (worker.isAlive()) {
  53.             runTime.setTimeout(keepAlive, 400);
  54.         } else {
  55.             printl('主线程:后台任务线程已退出');
  56.         }
  57.     })();
  58. }

  59. // ── 触发演示 ──
  60. demo_runJsFile();
复制代码











欢迎光临 B2B网络软件 (http://bbs.niubt.cn/) Powered by Discuz! X3.2