console.show();
const {
delivery,
interval,
quantity,
spec,
clickMode,
actionDelay
} = hamibot.env;
// 打印环境变量
toastLog('delivery = '+delivery);
toastLog('interval = '+interval);
toastLog('quantity = '+quantity);
toastLog('spec = '+spec);
toastLog('clickMode = '+clickMode);
toastLog('actionDelay = '+actionDelay);
// 创建悬浮窗并命名为 floatyWin
var floatyWin = floaty.window(
<vertical padding="16">
<button id="startBtn" text="开始任务" bg="#4CAF50" />
<button id="stopBtn" text="停止任务" bg="#F44336" enabled="false" />
<button id="moveBtn" text="移动脚本" bg="#607D8B" />
</vertical>
);
floatyWin.setAdjustEnabled(false);
floatyWin.setPosition(500, 100)
floatyWin.setSize(-2, -2);
var handlers = {
running: false,
taskInterval: null,
start: function () {
if (handlers.running) return;
handlers.running = true;
toastLog('任务开始了');
// 开始循环执行
handlers.taskInterval = setInterval(function () {
toastLog('状态:运行中');
var randomDelay = 5000 + Math.floor(Math.random() * 101);
if (className("android.widget.TextView").text("确定").exists()) {
// // className("android.widget.TextView").text("确定").findOne().click();
toastLog('点击了确定');
clearInterval(handlers.taskInterval);
} else {
if (className("android.widget.TextView").text("送到家").findOne().exists()){
className("android.widget.TextView").text("送到家").findOne().click();
sleep(randomDelay);
toastLog('点击了送到家,等待'+randomDelay+'ms');
}
if (className("android.widget.TextView").text("到店取").findOne().exists()){
className("android.widget.TextView").text("到店取").findOne().click();
sleep(randomDelay);
toastLog('点击了到店取,等待'+randomDelay+'ms');
}
}
}, 1000); // 每秒执行一次
},
stop: function () {
if (!handlers.running) return;
handlers.running = false;
clearInterval(handlers.taskInterval);
toastLog('任务停止');
},
};
floatyWin.startBtn.click(function () {
handlers.start();
ui.run(function () {
floatyWin.startBtn.enabled = false;
floatyWin.stopBtn.enabled = true;
});
});
floatyWin.stopBtn.click(function () {
handlers.stop();
ui.run(function () {
floatyWin.startBtn.enabled = true;
floatyWin.stopBtn.enabled = false;
});
});
var movestatus=false;
floatyWin.moveBtn.click(function () {
if(!movestatus){
movestatus=true;
ui.run(function () {
floatyWin.setAdjustEnabled(true);
});
}else{
movestatus=false;
floatyWin.setAdjustEnabled(false);
}
});
// 保持悬浮窗不被系统回收
setInterval(function () {}, 1000); 执行循环的过程中 如果添加列className("android.widget.TextView").text("送到家").findOne()类似的查找,脚本会奔溃,如果是空跑 程序运行正常,解决该问题。
回答
脚本在使用 className("android.widget.TextView").text("送到家").findOne()
进行控件查找时崩溃,通常是因为在某些情况下控件不存在或没有正确匹配,导致 findOne()
抛出异常。为了避免此类崩溃,应该在查找控件之前进行存在性检查。
以下是修复建议的代码:
// 文档地址 https://docs.hamibot.com/
var handlers = {
running: false,
taskInterval: null,
start: function () {
if (handlers.running) return;
handlers.running = true;
toastLog('任务开始了');
// 开始循环执行
handlers.taskInterval = setInterval(function () {
toastLog('状态:运行中');
var randomDelay = 5000 + Math.floor(Math.random() * 101);
// 使用 exists() 检查控件是否存在
if (className("android.widget.TextView").text("确定").exists()) {
className("android.widget.TextView").text("确定").findOne().click();
toastLog('点击了确定');
clearInterval(handlers.taskInterval);
} else {
// 添加空值判断和 try-catch 避免崩溃
try {
let deliveryWidget = className("android.widget.TextView").text("送到家").findOne(1000); // 等待最多1秒
if (deliveryWidget) {
deliveryWidget.click();
sleep(randomDelay);
toastLog('点击了送到家,等待'+randomDelay+'ms');
}
} catch (e) {
toastLog('送达家控件未找到或发生异常');
}
try {
let pickupWidget = className("android.widget.TextView").text("到店取").findOne(1000); // 等待最多1秒
if (pickupWidget) {
pickupWidget.click();
sleep(randomDelay);
toastLog('点击了到店取,等待'+randomDelay+'ms');
}
} catch (e) {
toastLog('到店取控件未找到或发生异常');
}
}
}, 1000); // 每秒执行一次
},
stop: function () {
if (!handlers.running) return;
handlers.running = false;
clearInterval(handlers.taskInterval);
toastLog('任务停止');
},
};
解释
-
避免直接调用
findOne()
:- 使用
findOne(timeout)
并设置超时时间(如1000
毫秒),以防止长时间阻塞。 - 在获取控件后添加
if (widget)
判断,确保控件存在后再执行操作。
- 使用
-
添加
try-catch
块:- 防止因控件未找到或其它异常导致脚本中断。
通过这些优化,即使控件不存在或查找失败,脚本也不会崩溃,而是继续下一轮循环。这样可以提升脚本的稳定性和健壮性。