YYPOST群发软件 发表于 11 小时前

原生UIAIWROK软件事件监听示例



原生UIAIWROK软件事件监听示例



/**
* AIWROK 事件监听示例
* 演示各种系统事件监听功能:广播、通知、按钮事件、Toast等
*/

var ac = new activity();
ac.loadSXML(`
<ScrollView
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <LinearLayout
      android:layout_width="match_parent"
      android:layout_height="wrap_content"
      android:orientation="vertical"
      android:padding="16dp"
      android:background="#f5f5f5">

    <TextView
      android:layout_width="match_parent"
      android:layout_height="wrap_content"
      android:text="AIWROK 事件监听示例"
      android:textSize="24sp"
      android:textStyle="bold"
      android:textColor="#333333"
      android:gravity="center"
      android:layout_marginBottom="10dp"/>

    <!-- QQ群信息 -->
    <LinearLayout
      android:layout_width="match_parent"
      android:layout_height="wrap_content"
      android:orientation="vertical"
      android:background="#FFF3E0"
      android:padding="10dp"
      android:layout_marginBottom="15dp">

      <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="&#127822;&#127822;:711841924"
            android:textSize="14sp"
            android:textColor="#E65100"
            android:gravity="center"
            android:layout_marginBottom="5dp"/>

      <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="&#127822;&#127822;2:528816639"
            android:textSize="14sp"
            android:textColor="#E65100"
            android:gravity="center"/>
    </LinearLayout>

    <!-- 监听状态显示 -->
    <TextView
      android:id="@+id/tvListenerStatus"
      android:layout_width="match_parent"
      android:layout_height="wrap_content"
      android:text="监听状态: 未启动"
      android:textSize="16sp"
      android:textColor="#666666"
      android:gravity="center"
      android:padding="10dp"
      android:background="#ffffff"
      android:layout_marginBottom="15dp"/>

    <!-- 广播事件监听 -->
    <TextView
      android:layout_width="match_parent"
      android:layout_height="wrap_content"
      android:text="&#128225; 广播事件监听"
      android:textSize="16sp"
      android:textStyle="bold"
      android:textColor="#333333"
      android:layout_marginBottom="10dp"/>

    <Button
      android:id="@+id/btnBroadcast"
      android:layout_width="match_parent"
      android:layout_height="wrap_content"
      android:text="发送测试广播"
      android:background="#2196F3"
      android:textColor="#ffffff"
      android:layout_marginBottom="10dp"/>

    <TextView
      android:id="@+id/tvBroadcastLog"
      android:layout_width="match_parent"
      android:layout_height="wrap_content"
      android:text="广播日志将显示在这里..."
      android:textSize="14sp"
      android:textColor="#666666"
      android:padding="10dp"
      android:background="#ffffff"
      android:layout_marginBottom="20dp"/>

    <!-- 通知事件监听 -->
    <TextView
      android:layout_width="match_parent"
      android:layout_height="wrap_content"
      android:text="&#128276; 通知事件监听"
      android:textSize="16sp"
      android:textStyle="bold"
      android:textColor="#333333"
      android:layout_marginBottom="10dp"/>

    <TextView
      android:layout_width="match_parent"
      android:layout_height="wrap_content"
      android:text="开启后将监听系统通知,按钮触发模拟通知"
      android:textSize="14sp"
      android:textColor="#999999"
      android:layout_marginBottom="10dp"/>

    <LinearLayout
      android:layout_width="match_parent"
      android:layout_height="wrap_content"
      android:orientation="horizontal"
      android:layout_marginBottom="10dp">

      <Button
            android:id="@+id/btnStartNotificationListener"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="开启监听"
            android:background="#4CAF50"
            android:textColor="#ffffff"
            android:layout_marginRight="5dp"/>

      <Button
            android:id="@+id/btnStopNotificationListener"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="关闭监听"
            android:background="#F44336"
            android:textColor="#ffffff"
            android:layout_marginLeft="5dp"/>
    </LinearLayout>

    <Button
      android:id="@+id/btnSimulateNotification"
      android:layout_width="match_parent"
      android:layout_height="wrap_content"
      android:text="模拟通知事件"
      android:background="#9C27B0"
      android:textColor="#ffffff"
      android:layout_marginBottom="10dp"/>

    <TextView
      android:id="@+id/tvNotificationLog"
      android:layout_width="match_parent"
      android:layout_height="wrap_content"
      android:text="通知日志将显示在这里..."
      android:textSize="14sp"
      android:textColor="#666666"
      android:padding="10dp"
      android:background="#ffffff"
      android:layout_marginBottom="20dp"/>

    <!-- 按钮事件监听 -->
    <TextView
      android:layout_width="match_parent"
      android:layout_height="wrap_content"
      android:text="⏸️ 暂停/停止按钮事件"
      android:textSize="16sp"
      android:textStyle="bold"
      android:textColor="#333333"
      android:layout_marginBottom="10dp"/>

    <TextView
      android:layout_width="match_parent"
      android:layout_height="wrap_content"
      android:text="监听脚本运行时的暂停和停止按钮"
      android:textSize="14sp"
      android:textColor="#999999"
      android:layout_marginBottom="10dp"/>

    <Button
      android:id="@+id/btnTestPause"
      android:layout_width="match_parent"
      android:layout_height="wrap_content"
      android:text="模拟暂停按钮"
      android:background="#FF9800"
      android:textColor="#ffffff"
      android:layout_marginBottom="10dp"/>

    <Button
      android:id="@+id/btnTestStop"
      android:layout_width="match_parent"
      android:layout_height="wrap_content"
      android:text="模拟停止按钮"
      android:background="#F44336"
      android:textColor="#ffffff"
      android:layout_marginBottom="10dp"/>

    <TextView
      android:id="@+id/tvBtnEventLog"
      android:layout_width="match_parent"
      android:layout_height="wrap_content"
      android:text="按钮事件日志将显示在这里..."
      android:textSize="14sp"
      android:textColor="#666666"
      android:padding="10dp"
      android:background="#ffffff"
      android:layout_marginBottom="20dp"/>

    <!-- Toast事件监听 -->
    <TextView
      android:layout_width="match_parent"
      android:layout_height="wrap_content"
      android:text="&#127838; Toast事件监听"
      android:textSize="16sp"
      android:textStyle="bold"
      android:textColor="#333333"
      android:layout_marginBottom="10dp"/>

    <TextView
      android:layout_width="match_parent"
      android:layout_height="wrap_content"
      android:text="监听脚本中显示的Toast消息"
      android:textSize="14sp"
      android:textColor="#999999"
      android:layout_marginBottom="10dp"/>

    <Button
      android:id="@+id/btnShowToast"
      android:layout_width="match_parent"
      android:layout_height="wrap_content"
      android:text="显示Toast消息"
      android:background="#795548"
      android:textColor="#ffffff"
      android:layout_marginBottom="10dp"/>

    <TextView
      android:id="@+id/tvToastLog"
      android:layout_width="match_parent"
      android:layout_height="wrap_content"
      android:text="Toast日志将显示在这里..."
      android:textSize="14sp"
      android:textColor="#666666"
      android:padding="10dp"
      android:background="#ffffff"
      android:layout_marginBottom="20dp"/>

    <!-- 热修复完成事件 -->
    <TextView
      android:layout_width="match_parent"
      android:layout_height="wrap_content"
      android:text="&#128295; 热修复完成事件"
      android:textSize="16sp"
      android:textStyle="bold"
      android:textColor="#333333"
      android:layout_marginBottom="10dp"/>

    <Button
      android:id="@+id/btnSimulateHotfix"
      android:layout_width="match_parent"
      android:layout_height="wrap_content"
      android:text="模拟热修复完成"
      android:background="#009688"
      android:textColor="#ffffff"
      android:layout_marginBottom="10dp"/>

    <TextView
      android:id="@+id/tvHotfixLog"
      android:layout_width="match_parent"
      android:layout_height="wrap_content"
      android:text="热修复日志将显示在这里..."
      android:textSize="14sp"
      android:textColor="#666666"
      android:padding="10dp"
      android:background="#ffffff"
      android:layout_marginBottom="20dp"/>

    <!-- 脚本停止事件 -->
    <TextView
      android:layout_width="match_parent"
      android:layout_height="wrap_content"
      android:text="⏹️ 脚本停止事件"
      android:textSize="16sp"
      android:textStyle="bold"
      android:textColor="#333333"
      android:layout_marginBottom="10dp"/>

    <Button
      android:id="@+id/btnSimulateStop"
      android:layout_width="match_parent"
      android:layout_height="wrap_content"
      android:text="模拟脚本停止"
      android:background="#607D8B"
      android:textColor="#ffffff"
      android:layout_marginBottom="10dp"/>

    <TextView
      android:id="@+id/tvStopLog"
      android:layout_width="match_parent"
      android:layout_height="wrap_content"
      android:text="停止事件日志将显示在这里..."
      android:textSize="14sp"
      android:textColor="#666666"
      android:padding="10dp"
      android:background="#ffffff"
      android:layout_marginBottom="20dp"/>

    <!-- 日志显示区域 -->
    <TextView
      android:layout_width="match_parent"
      android:layout_height="wrap_content"
      android:text="&#128203; 综合日志"
      android:textSize="16sp"
      android:textStyle="bold"
      android:textColor="#333333"
      android:layout_marginBottom="10dp"/>

    <TextView
      android:id="@+id/tvLog"
      android:layout_width="match_parent"
      android:layout_height="200dp"
      android:text="综合日志将显示在这里...\n\n提示:\n1. 点击上方各区域按钮可模拟对应事件\n2. 实际使用时这些事件由系统自动触发\n3. 本示例帮助你理解事件监听的使用方法"
      android:textSize="14sp"
      android:textColor="#666666"
      android:padding="10dp"
      android:background="#ffffff"
      android:scrollbars="vertical"
      android:gravity="top"/>

    </LinearLayout>

</ScrollView>
`);

// 日志悬浮窗
var 日志悬浮窗 = new floatUI();
日志悬浮窗.loadXML(
    '<LinearLayout orientation="vertical" w="8" h="8" gravity="center">' +
    '<TextView id="rz" textColor="#ff3ee4" background="#000000" layout_width="wrap_content" layout_height="wrap_content" />' +
    '</LinearLayout>'
);
var g_日志 = 日志悬浮窗.findViewById('rz');
if (g_日志) {
    setTimeout(function() {
      日志悬浮窗.setPosition(0, (screen.getScreenHeight() * 0.4));
      g_日志.setText("监听示例已启动");
    }, 100);
}

// 获取控件
var tvListenerStatus = ac.findViewById("tvListenerStatus");
var btnBroadcast = ac.findViewById("btnBroadcast");
var tvBroadcastLog = ac.findViewById("tvBroadcastLog");
var btnStartNotificationListener = ac.findViewById("btnStartNotificationListener");
var btnStopNotificationListener = ac.findViewById("btnStopNotificationListener");
var btnSimulateNotification = ac.findViewById("btnSimulateNotification");
var tvNotificationLog = ac.findViewById("tvNotificationLog");
var btnTestPause = ac.findViewById("btnTestPause");
var btnTestStop = ac.findViewById("btnTestStop");
var tvBtnEventLog = ac.findViewById("tvBtnEventLog");
var btnShowToast = ac.findViewById("btnShowToast");
var tvToastLog = ac.findViewById("tvToastLog");
var btnSimulateHotfix = ac.findViewById("btnSimulateHotfix");
var tvHotfixLog = ac.findViewById("tvHotfixLog");
var btnSimulateStop = ac.findViewById("btnSimulateStop");
var tvStopLog = ac.findViewById("tvStopLog");
var tvLog = ac.findViewById("tvLog");

// 标记脚本是否正在运行
var isScriptRunning = true;

// 日志记录函数
function logToAll(source, message) {
    // 输出到控制台(总是执行)
    console.log("[" + source + "] " + message);

    // 如果脚本已停止或UI已销毁,不再更新界面
    if (!isScriptRunning || !tvLog) {
      return;
    }

    var timestamp = new java.text.SimpleDateFormat("HH:mm:ss").format(new java.util.Date());
    var logEntry = "[" + timestamp + "] " + message;

    // 更新综合日志
    try {
      var currentLog = tvLog.getText();
      if (!currentLog) {
            currentLog = "";
      }
      currentLog = String(currentLog);
      var lines = currentLog.split("\n");
      // 过滤空行
      while (lines.length > 0 && lines === "") {
            lines.shift();
      }
      if (lines.length > 50) {
            lines = lines.slice(-50);
      }
      lines.push(logEntry);
      tvLog.setText(lines.join("\n"));
    } catch (e) {
      // UI可能已销毁,忽略错误
      console.log("更新日志失败: " + e);
    }

    // 更新悬浮窗
    if (g_日志) {
      try {
            g_日志.setText(source + ": " + message);
      } catch (e) {
            // 忽略
      }
    }
}

// 安全设置文本的辅助函数
function safeSetText(view, text) {
    if (!isScriptRunning || !view) {
      return;
    }
    try {
      view.setText(text);
    } catch (e) {
      // UI可能已销毁,忽略错误
    }
}

// 更新监听状态
function updateStatus(status) {
    console.log("监听状态: " + status);
    safeSetText(tvListenerStatus, "监听状态: " + status);
}

// ==================== 设置事件监听器 ====================

// 1. 广播事件监听
try {
    event.onBroadcastEvent(function(msg) {
      logToAll("广播", "收到广播: " + msg);
      safeSetText(tvBroadcastLog, "收到广播: " + msg);
    });
    logToAll("系统", "✅ 广播事件监听器已设置");
} catch (e) {
    logToAll("错误", "设置广播监听失败: " + e);
}

// 2. 通知事件监听
var notificationListenerEnabled = false;

try {
    event.onNotificationEvent(function(notification) {
      try {
            var title = notification.getTitle ? notification.getTitle() : "无标题";
            var text = notification.getText ? notification.getText() : "无内容";
            logToAll("通知", "收到通知 - 标题: " + title + ", 内容: " + text);
            safeSetText(tvNotificationLog, "收到通知\n标题: " + title + "\n内容: " + text);
      } catch (e) {
            logToAll("通知", "处理通知失败: " + e);
      }
    });
    logToAll("系统", "✅ 通知事件监听器已设置");
} catch (e) {
    logToAll("错误", "设置通知监听失败: " + e);
}

// 3. 暂停按钮事件监听
try {
    event.onPauseBtnEvent(function() {
      logToAll("按钮", "暂停按钮被点击");
      safeSetText(tvBtnEventLog, "暂停按钮被点击\n时间: " + new java.text.SimpleDateFormat("HH:mm:ss").format(new java.util.Date()));
    });
    logToAll("系统", "✅ 暂停按钮事件监听器已设置");
} catch (e) {
    logToAll("错误", "设置暂停按钮监听失败: " + e);
}

// 4. 停止按钮事件监听
try {
    event.onStopBtnEvent(function() {
      logToAll("按钮", "停止按钮被点击");
      safeSetText(tvBtnEventLog, "停止按钮被点击\n时间: " + new java.text.SimpleDateFormat("HH:mm:ss").format(new java.util.Date()));
    });
    logToAll("系统", "✅ 停止按钮事件监听器已设置");
} catch (e) {
    logToAll("错误", "设置停止按钮监听失败: " + e);
}

// 5. 脚本停止事件监听
try {
    event.onStopEvent(function() {
      isScriptRunning = false;
      console.log("脚本停止事件触发,停止更新UI");
      safeSetText(tvStopLog, "脚本停止事件触发\n时间: " + new java.text.SimpleDateFormat("HH:mm:ss").format(new java.util.Date()));
    });
    logToAll("系统", "✅ 脚本停止事件监听器已设置");
} catch (e) {
    logToAll("错误", "设置脚本停止监听失败: " + e);
}

// 6. Toast事件监听
try {
    event.onToastEvent(function(msg) {
      logToAll("Toast", "收到Toast: " + msg);
      safeSetText(tvToastLog, "收到Toast: " + msg);
    });
    logToAll("系统", "✅ Toast事件监听器已设置");
} catch (e) {
    logToAll("错误", "设置Toast监听失败: " + e);
}

// 7. 热修复完成事件监听
try {
    event.onHotfixFinish(function(success) {
      var result = success ? "成功" : "失败";
      logToAll("热修复", "热修复完成,结果: " + result);
      safeSetText(tvHotfixLog, "热修复完成\n结果: " + result + "\n时间: " + new java.text.SimpleDateFormat("HH:mm:ss").format(new java.util.Date()));
    });
    logToAll("系统", "✅ 热修复完成事件监听器已设置");
} catch (e) {
    logToAll("错误", "设置热修复监听失败: " + e);
}

// ==================== 按钮点击事件 ====================

// 发送测试广播
btnBroadcast.setOnClickListener(function() {
    logToAll("测试", "发送测试广播");
    safeSetText(tvBroadcastLog, "已发送测试广播\n时间: " + new java.text.SimpleDateFormat("HH:mm:ss").format(new java.util.Date()));
    // 实际发送广播
    try {
      if (typeof broadcast !== 'undefined') {
            broadcast.send("test_broadcast");
      }
    } catch (e) {
      logToAll("错误", "发送广播失败: " + e);
    }
});

// 开启通知监听
btnStartNotificationListener.setOnClickListener(function() {
    notificationListenerEnabled = true;
    logToAll("测试", "通知监听已开启");
    safeSetText(tvNotificationLog, "通知监听已开启\n等待系统通知...");
});

// 关闭通知监听
btnStopNotificationListener.setOnClickListener(function() {
    notificationListenerEnabled = false;
    logToAll("测试", "通知监听已关闭");
    safeSetText(tvNotificationLog, "通知监听已关闭");
});

// 模拟通知事件
btnSimulateNotification.setOnClickListener(function() {
    logToAll("测试", "模拟通知事件");
    safeSetText(tvNotificationLog, "模拟通知\n标题: 测试通知\n内容: 这是一条模拟的通知消息\n时间: " + new java.text.SimpleDateFormat("HH:mm:ss").format(new java.util.Date()));
});

// 模拟暂停按钮
btnTestPause.setOnClickListener(function() {
    logToAll("测试", "模拟暂停按钮点击");
    safeSetText(tvBtnEventLog, "模拟暂停按钮点击\n时间: " + new java.text.SimpleDateFormat("HH:mm:ss").format(new java.util.Date()));
});

// 模拟停止按钮
btnTestStop.setOnClickListener(function() {
    logToAll("测试", "模拟停止按钮点击");
    safeSetText(tvBtnEventLog, "模拟停止按钮点击\n时间: " + new java.text.SimpleDateFormat("HH:mm:ss").format(new java.util.Date()));
});

// 显示Toast
btnShowToast.setOnClickListener(function() {
    var toastMsg = "测试Toast消息 - " + new java.text.SimpleDateFormat("HH:mm:ss").format(new java.util.Date());
    logToAll("测试", "显示Toast: " + toastMsg);
    safeSetText(tvToastLog, "显示Toast: " + toastMsg);
    // 实际显示Toast(使用兼容方式)
    try {
      // 尝试使用ui.toast方法
      if (typeof ui !== 'undefined' && typeof ui.toast === 'function') {
            ui.toast(toastMsg);
      }
      // 尝试使用toast.show方法
      else if (typeof toast !== 'undefined' && typeof toast.show === 'function') {
            toast.show(toastMsg);
      }
      // 如果以上方法都不可用,使用printl输出消息
      else {
            printl("Toast消息: " + toastMsg);
      }
    } catch (e) {
      logToAll("错误", "显示Toast失败: " + e);
      // 出错时使用printl作为备用方案
      printl("Toast消息: " + toastMsg);
    }
});

// 模拟热修复完成
btnSimulateHotfix.setOnClickListener(function() {
    logToAll("测试", "模拟热修复完成事件");
    safeSetText(tvHotfixLog, "模拟热修复完成\n结果: 成功\n时间: " + new java.text.SimpleDateFormat("HH:mm:ss").format(new java.util.Date()));
});

// 模拟脚本停止
btnSimulateStop.setOnClickListener(function() {
    logToAll("测试", "模拟脚本停止事件");
    safeSetText(tvStopLog, "模拟脚本停止事件\n时间: " + new java.text.SimpleDateFormat("HH:mm:ss").format(new java.util.Date()));
});

// 初始化完成
updateStatus("已启动");
logToAll("系统", "事件监听示例已启动,所有监听器已设置完毕");

console.log("✅ AIWROK事件监听示例已启动");


页: [1]
查看完整版本: 原生UIAIWROK软件事件监听示例